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?
find raw into the RegExp constructor, the various regex special characters will cause trouble. For instance, if find is "Mr. Smith" you'll replace "Mrs Smith" as well.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/
:ID:.
str