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
Solved

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

Posted on 2016-11-04
2
76 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
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

856 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