Javascript string replace is a very useful function. Javascript has a built-in string replace function but it uses regular expressions. Here you will fint two versions of custom string replace functions (maybe more wrappers for built-in functions).
Source code for webtoolkit.strreplace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/**
*
* Javascript string replace
* http://www.webtoolkit.info/
*
**/
// standart string replace functionality
function str_replace(haystack, needle, replacement) {
var temp = haystack.split(needle);
return temp.join(replacement);
}
// needle may be a regular expression
function str_replace_reg(haystack, needle, replacement) {
var r = new RegExp(needle, 'g');
return haystack.replace(r, replacement);
}
|