我正在尝试删除单词末尾的感叹号。
例如。remove("!!!Hi !!hi!!!hi!hi") === "!!!Hi !!hi !hi"
我可以删除所有感叹号,但无法定位单词末尾的感叹号。
以下是我目前拥有的。
function remove(s){
return s.replace(/([a-z]+)[!]/ig, '$1');
}
最佳答案
您可以使用以下正则表达式去掉单词末尾的 !
:
"!!!Hi !!hi!!! !hi"
.replace(/!+\s/g, ' ') // this removes it from end of words
.replace(/!+$/g, '') // this removes it from the end of the last word
结果:“!!!嗨!!嗨!嗨”
关于javascript - 正则表达式仅删除单词末尾的字符 - js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45320342/