使用javascript删除除字母数字和空格以外的所有字符 [英] Remove all characters except alphanumeric and spaces with javascript
本文介绍了使用javascript删除除字母数字和空格以外的所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我喜欢所提供的解决方案从字符串中删除不是字母数字字符。遇到[\]字符时出现问题但是如何在保留空格的情况下执行此操作?
I like the solution povided by "Remove not alphanumeric characters from string. Having trouble with the [\] character" but how would I do this while leaving the spaces in place?
我需要根据清空后的空格来标记字符串。
I need to tokenize string based on the spaces after it has been cleaned.
推荐答案
input.replace(/[^\w\s]/gi, '')
从另一个答案中无耻地偷走了。字符类中的 ^
表示不是。所以这是不是 \w
(相当于 \W
)而不是 \s
,这是空格字符(空格,制表符等)。如果需要,您可以使用文字
。
Shamelessly stolen from the other answer. ^
in the character class means "not." So this is "not" \w
(equivalent to \W
) and not \s
, which is space characters (spaces, tabs, etc.) You can just use the literal if you need.
这篇关于使用javascript删除除字母数字和空格以外的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文