我有一个字符串测试="你好,你们都在做什么,我希望它很好!很好.期待见到你.
我试图使用javascript将字符串解析为单词和标点符号.我可以分开单词但是标点符号会使用正则表达式消失
var result = test.match(/\b(\ w |')+\b/g);
所以我的预期输出是
hello
how
are
you
all
doing
,
I
hope
that
it's
good
!
and
fine
.
Looking
forward
to
see
you
Run Code Online (Sandbox Code Playgroud)
Kev*_*Cox 10
如果你这第一种方法,和javascript的"单词"定义匹配.下面是一种更可定制的方法.
试试test.split(/\s*\b\s*/).它在单词边界(\b)上分裂并吃掉空格.
"hello how are you all doing, I hope that it's good! and fine. Looking forward to see you."
.split(/\s*\b\s*/);
// Returns:
["hello",
"how",
"are",
"you",
"all",
"doing",
",",
"I",
"hope",
"that",
"it",
"'",
"s",
"good",
"!",
"and",
"fine",
".",
"Looking",
"forward",
"to",
"see",
"you",
"."]
Run Code Online (Sandbox Code Playgroud)
var test = "This is. A test?"; // Test string.
// First consider splitting on word boundaries (\b).
test.split(/\b/); //=> ["This"," ","is",". ","A"," ","test","?"]
// This almost works but there is some unwanted whitespace.
// So we change the split regex to gobble the whitespace using \s*
test.split(/\s*\b\s*/) //=> ["This","is",".","A","test","?"]
// Now the whitespace is included in the separator
// and not included in the result.
Run Code Online (Sandbox Code Playgroud)
如果你想要将"isn`t"和"one-thousand"这样的单词视为单个单词,而javascript正则表达式将它们视为两个单词,则需要创建自己的单词定义.
test.match(/[\w-']+|[^\w\s]+/g) //=> ["This","is",".","A","test","?"]
Run Code Online (Sandbox Code Playgroud)
这使用交替分别匹配标点符号的实际单词.正则表达式的前半部分[\w-']+匹配您认为是单词的任何内容,而后半部分[^\w\s]+匹配您认为标点符号的任何内容.在这个例子中,我只使用了不是单词或空格的东西.我也只是一个+结尾,以便多字符标点符号(例如?!正确写入!)被视为单个字符,如果你不想删除它+.