替换指定字符串

1 使用切割-连接的方式

function replaceSpace( s ) {
    // write code here
    return s.split(' ').join('%20')
}
module.exports = {
    replaceSpace : replaceSpace
};

 

2 使用替换的方式

function replaceSpace( s ) {
    // write code here
    return s.replace(/\s/g,'%20');
    return s.replace(/ /g,'%20'); // 正常表达式 /***/g 中间是要求的东西,如果需要转义 则加一个\,例如/\//g
}
module.exports = {
    replaceSpace : replaceSpace
};

详细正则表达式,请看

https://www.runoob.com/regexp/regexp-syntax.html

posted @ 2021-04-08 10:55  小菜张!  阅读(182)  评论(0)    收藏  举报