Replacement string of characters
1.regExp: regular expression matching string, matching the success of the specified character string replaced.
2.str: Replace with the specified character string str.
3.string: replacement string
str.replace(regExp | str , string);
Examples
Replace all spaces in a string
function replaceStr(val){
var str = val.replace(/\s/g,'');
return str;
};
console.log( replaceStr('a b c b') ); // 'abcb'
At the beginning of the end of a string to replace all spaces
function replaceStr(val){
var str = val.replace(/^\s* | *\s$/g,'');
return str;
};
console.log( 'test1'+replaceStr(' a b c b ') + 'test2' ); // 'test1a b c btest2'