Solved

How to replace a token in a string with square brackets?

Posted on 2016-11-04
2
103 Views
Last Modified: 2016-11-04
I'm trying to replace the token "[RANDOMNUMBER]" in a string but my code is not working? I've tried single and double backslashes and it hasn't worked?
function detokenizeTags(imgSrc){
	var rn = Math.random() + "",
        rnd = rn * 10000000000000;
	imgSrc.replace(/\[RANDOMNUMBER\]/g,rnd);
        return imgSrc; 
    }

Open in new window


sample string:   //ad.amgdgt.com/ads/?t=ap&px=79079&rnd=[RANDOMNUMBER]
0
Comment
Question by:894359
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 41875127
From MDN:
Description:
This method does not change the String object it is called on. It simply returns a new string.
So you need to return the result of the replace from your function instead of the original string object:
function detokenizeTags(imgSrc){
	var rn = Math.random() + "",
        rnd = rn * 10000000000000;
	return imgSrc.replace(/\[RANDOMNUMBER\]/g,rnd);
    }

Open in new window

Here's the fiddle I tested with: https://jsfiddle.net/2xLwpu75/
0
 

Author Closing Comment

by:894359
ID: 41875131
I knew it was something stupid I overlooked! That's what I get for being up almost 2 days straight now! Much appreciated!
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question