与分割不同,将其视为提取包含分隔符或连续字符的字符串可能更容易。not分隔符:
'asdf a b c2 '.match(/\S+|\s/g)
// result: ["asdf", " ", "a", " ", " ", "b", " ", "c2", " "]
'asdf a b. . c2% * '.match(/\S+|\s/g)
// result: ["asdf", " ", "a", " ", " ", "b.", " ", ".", " ", "c2%", " ", "*", " "]
对比赛的更莎士比亚式的定义是:
'asdf a b c2 '.match(/ |[^ ]+/g)
To
或(不
)+.