gpt4 book ai didi

javascript - 正则表达式仅删除单词末尾的字符 - js

转载 作者:行者123 更新时间:2023-12-01 03:19:30 25 4
gpt4 key购买 nike

我正在尝试删除单词末尾的感叹号。

例如。 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com