[英]Javascript remove all characters from string which are not numbers, letters and whitespace
With JavaScript I would like to remove all characters from a string which are not numbers, letters and whitespace. 使用JavaScript我想删除字符串中不是数字,字母和空格的所有字符。 So remove characters like
'%#$&@*
so something like: 所以删除
'%#$&@*
等字符,如下所示:
Player's got to * play!
玩家必须玩! #justsaying
#justsaying
would become: 会成为:
Players got to play justsaying
玩家必须玩得很开心
How can I do this, I'm not sure about regex. 我怎么能这样做,我不确定正则表达式。
As @chris85 said, you can use the regular expression [^0-9a-zAZ]
to replace all the characters that are not letters, numbers, or whitespace. 正如@ chris85所说,你可以使用正则表达式
[^0-9a-zAZ]
来替换所有不是字母,数字或空格的字符。
Here is a function that will do what you want: 这是一个可以做你想要的功能:
function clean(str) {
return str.replace(/[^0-9a-z-A-Z ]/g, "").replace(/ +/, " ")
}
The second replace call is needed to remove the runs of extra whitespace that are made from removing a character that is between spaces. 需要第二次替换调用来删除通过删除空格之间的字符而产生的额外空格的运行。
Use replace
使用
replace
string.replace(/[^A-Z\d\s]/gi, '')
Note the two flags at the end of the regex 注意正则表达式末尾的两个标志
g
- stands for global, and means that every such instance of the regular expression will be found g
- 代表全局,意味着将找到正则表达式的每个这样的实例
i
- stands for case insensitive. i
- 代表不区分大小写。 That means it will match both lower case and uppercase characters 这意味着它将匹配小写和大写字符
Playing with your string, it returns this output 使用您的字符串,它返回此输出
"Players got to play justsaying"
To transform two or more whitespace characters into a single whitespace, you could hain another replace
method to the existing ones. 要将两个或多个空白字符转换为单个空格,您可以使用另一种
replace
现有方法。
string.replace(/[^A-Z\d\s]/gi, '').replace(/\s+/g, ' ')
The key here is the +
character, which finds one or more. 这里的关键是
+
字符,它找到一个或多个。
It's probably possible to do it more efficiently, but I'm an amateur in Regex. 可能更有效率地做到这一点,但我是Regex的业余爱好者。
ORIGINAL TEXT 原文
Player's got to * play! #justsaying
REGEXP REGEXP
([^a-zA-Z0-9\s])
RESULT 结果
Players got to play justsaying
TEST IT 测试它
const regex = /([^a-zA-Z0-9\\s])/gm; const str = `Player's got to * play! #justsaying`; const subst = ``; // The substituted value will be contained in the result variable const result = str.replace(regex, subst); console.log(result);
See: https://regex101.com/r/BYSDwz/4 请参阅: https : //regex101.com/r/BYSDwz/4
I also added sign '
to regex because you have word Player's
, so here my code: 我还为正则表达式添加了符号
'
因为你有单词Player's
,所以这里是我的代码:
var str = "Player's got to * play! #justsaying";
var result = str.replace(/[^a-z\d\s']/gi, '');
Try with 试试吧
var text = "Player's got to * play! #justsaying";
var result = text.replace(/[^A-Z\d\s]/gi,'');
console.log(result);
Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 阅读更多内容: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
This is how you might go about matching the characters you are interested in removing... 这就是你如何匹配你有兴趣删除的角色......
[^\d\w\s]
NB: That you should use the global modifier 注意:您应该使用全局修饰符
Tested here: https://regex101.com/r/yilfcn/1 Refer to this post regarding how to apply it: javascript regexp remove all special characters 在这里测试: https : //regex101.com/r/yilfcn/1请参阅这篇文章关于如何应用它: javascript regexp删除所有特殊字符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.