Website Tips
Tuesday, 9 November 2010
Javascript Replace All Values Using A Variable
Here is how you replace all values in a string with another string which is inside of a variable using Javascript:
var str = "Please change all the change words in this sentence.";
var searchfor = "change";
str = str.replace(new RegExp(searchfor, 'g'), "changed");
The resulting value for str would be:
"Please changed all the changed words in this sentence.";
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment