I can't figure out why this code isn't working.
I want to replace a placeholder with an actual value. Sample code as below:
var str = "ID: :ID:";
str.replace(":ID:", 0);
console.log(str);
Output:
ID: :ID:
This is my JSFiddle
I can't figure out why this code isn't working.
I want to replace a placeholder with an actual value. Sample code as below:
var str = "ID: :ID:";
str.replace(":ID:", 0);
console.log(str);
Output:
ID: :ID:
This is my JSFiddle
The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.
var str = "ID: :ID:";
str = str.replace(":ID:", 0);
console.log(str);
P.s. replace only replaces the first occurrence.
Use replace all if you want to replace all occurrences:
How to replace all occurrences of a string in JavaScript?
Your just replacing the string, but not storing it back. So, it is giving previous string rather than new one.
var str = "ID: :ID:";
str = str.replace(":ID:", 0);
console.log(str);
JS Fiddle: http://jsfiddle.net/B9n79/2/
Try this:
var str = "ID: :ID:";
str = str.replace(/:ID:/gi, "0");
console.log(str);