Solved

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

Posted on 2016-11-04
2
30 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
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now