I want to use str_replace
or its similar alternative to replace some text in JavaScript.
var text = "this is some sample text that i want to replace";
var new_text = replace_in_javascript("want", "dont want", text);
document.write("new_text");
should give
this is some sample text that i dont want to replace
If you are going to regex, what are the performance implications in comparison to the built in replacement methods.
You do not need additional libraries.
Use JS
String.prototype.replace
first argument should be Regex pattern or String and Second argument should be a String or function.str.replace(regexp|substr, newSubStr|function);
Ex:
var str = 'this is some sample text that i want to replace'; var newstr = str.replace(/want/i, "dont't want"); document.write(newstr); // this is some sample text that i don't want to replace