我正试图在字末尾删除感叹号。正则表达式在字末尾删除字符 - js
例如。 remove("!!!Hi !!hi!!! !hi") === "!!!Hi !!hi !hi"
我能够删除所有感叹号,但无法将其定位在单词末尾。
以下是我目前拥有的。
function remove(s){
return s.replace(/([a-z]+)[!]/ig, '$1');
}
我正试图在字末尾删除感叹号。正则表达式在字末尾删除字符 - js
例如。 remove("!!!Hi !!hi!!! !hi") === "!!!Hi !!hi !hi"
我能够删除所有感叹号,但无法将其定位在单词末尾。
以下是我目前拥有的。
function remove(s){
return s.replace(/([a-z]+)[!]/ig, '$1');
}
可以剥离!
S中的在使用以下正则表达式的话结束:
"!!!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
结果:"!!!Hi !!hi !hi"
您正则表达式更改为:
/([a-z]+)!+/ig
Then
function remove(s){
return s.replace(/([a-z]+)!+/ig, '$1');
}
应该工作
你可以使用这个表达式
console.log("!!!Hi !!hi!!! !hi!!!".replace(/([a-z]+)[!]+/ig, '$1'))
这是[ @ idmean的答案](https://stackoverflow.com/a/45320428/3832970)的副本,没有附加价值。 –
你不能试试这个:
\b!+
它匹配!
后面一个字。
我认为这将是更容易不使用正则表达式,但insted的使用lastIndexOf和切片
类似:
function removeQuestionmark(inputvalue) {
var index = inputValue.lastIndexOf("!");
if(inputValue.endsWith("!"))
return inputvalue.slice(0,index-1);
return `${inputvalue.slice(0,index-1)${inputValue.slice(index+1)}}
}
我没有测试的代码。
那么你应该添加一个+,让你多一个!
function remove(s){
return s.replace(/([a-z]+)([!]+)/ig, "$1");
}
我想你可以使用['.replace(/ \ b!+ \ B /克, '')'](https://regex101.com/r/ugHixG/1) –