javascript basis - the replacement string text box

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'
Published 25 original articles · won praise 31 · views 110 000 +

Guess you like

Origin blog.csdn.net/qq_33236453/article/details/51604331