[英]Remove all special characters except for @ symbol from string in JavaScript
I have a string :我有一个字符串:
“Gazelles were mentioned by @JohnSmith while he had $100 in his pocket and screamed W#$@%@$!!!!"
I need:我需要:
“Gazelles were mentioned by @JohnSmith while he had 100 in his pocket and screamed"
How to remove all special characters from string EXCEPT the @ symbol.如何从字符串中删除除@ 符号之外的所有特殊字符。 I tried:
我试过:
str.replace(/[^\w\s]/gi, '')
If you want to keep the @
when it is followed by a word char and keeping the W
is also ok and also remove the newlines, you could for example change the \\s
to match spaces or tabs [ \\t]
如果您想在
@
后跟一个单词字符时保留@
并保留W
也可以并删除换行符,例如,您可以更改\\s
以匹配空格或制表符[ \\t]
Add the @
to the negated character class and use an alternation specifying to only match the @
when it is not followed by a word character using a negative lookahead.将
@
添加到否定字符类并使用替代指定仅在@
后面没有使用否定前瞻的单词字符时匹配。
[^\w \t@]+|@(?!\w)
[^\\w \\t@]+
Match 1+ times any char except a word char, space or tab [^\\w \\t@]+
匹配 1+ 次除字符、空格或制表符以外的任何字符|
Or@(?!\\w)
Match an @ not directly followed by a word char @(?!\\w)
匹配不直接跟在字符字符后面的@In the replacement use an empty string.在替换中使用空字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.