js正则表达式匹配字符串中所有空格

本文共有358个字,关键词:

问题:js如何替换掉字符串中的所有空格,replaceAll的方法有没有?
解决:js中只有replce方法,没有replaceAll方法,replce方法只能替换第一个字符串,但可以使用正则表达式的g功能,进行替换所有
方法:
str = 'undo redo |removeformat |-tool1-tool2-image-link unlink |-emotion-formula';
str.repalce(/[ ]/g, '-').repalce(/|/g,'-');
这样就可以把字符串中的空格和竖杠全都替换成横杠了
结果:undo-redo--removeformat---tool1-tool2-image-link-unlink---emotion-formula

版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。
添加新评论
暂无评论