我有一个javascript函数,我试图匹配长字符串中的每个单词,我有匹配工作基本上只是在空格上爆炸,虽然我想修改我的正则表达式自动删除特殊字符,如! , - 等。
例如:
var a = 'This! is a sample sentence, only words should be extracted from it!!';
我正在寻找正则表达式的结果
{This,is,a,sample,sentence,only,words,should,be,extracted,from,it}
答案 0 :(得分:2)
试试这个
> var a = 'This! is a sample sentence, only words should be extracted from it!!';
> a.match(/\w+/g);
["This", "is", "a", "sample", "sentence", "only", "words", "should", "be", "extracted", "from", "it"]
答案 1 :(得分:2)
不考虑拆分,而是考虑“匹配”“字字符:
var a = 'This! is a sample sentence, only words should be extracted from it!!';
a.match(/\w+/g);
//["This", "is", "a", "sample", "sentence", "only", "words", "should", "be", "extracted", "from", "it"]
只抓取单词字符表示不在字符类[A-Za-z0-9_]
中的任何内容被删除并视为分隔符。