Solved

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

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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to count occurrences of each item in an array.
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…

630 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