javascript - 正则表达式仅匹配具有特殊字符的长字符串中的单词

标签 javascript regex string

我有一个 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}

最佳答案

试试这个

> 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"]

关于javascript - 正则表达式仅匹配具有特殊字符的长字符串中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34869299/

相关文章:

javascript - Dispatch 不将操作发送到 reducer

regex - mod重写的奇怪错误

Javascript Replace() 在 iPhone 上对于长字符串表现不佳

c# - 通过 LINQ 使用通用列表中的值格式化字符串

mysql - 修剪列中逗号分隔值中的前导零

c++ - 如何获取 std::string 中的字符数?

javascript - `add` 但具有 "mutating"属性,例如 `push`

javascript - 有没有办法在控制台关闭的情况下让 console.log() 在 IE 中记录消息?

javascript - 将变量传递给回调函数的正确方法

javascript - 正则表达式,字符串中#valueN#的所有条目