Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-11-04
2
Medium Priority
?
124 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

926 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