繁体   English   中英

将字符串拆分为JavaScript中的单词,标点符号和空格数组

[英]Split a string into an array of words, punctuation and spaces in JavaScript

我有一个字符串,我想分成数组中包含的项目,如下例所示:

var text = "I like grumpy cats. Do you?"

// to result in:

var wordArray = ["I", " ", "like", " ", "grumpy", " ", "cats", ".", "  ", "Do", " ", "you", "?" ]

我尝试了以下表达式(和类似的变种没有成功

var wordArray = text.split(/(\S+|\W)/)
//this disregards spaces and doesn't separate punctuation from words

在Ruby中有一个正则表达式运算符(\\ b),它在任何单词边界处分割,保留空格和标点符号,但我找不到类似的Java脚本。 非常感谢你的帮助。

String#match方法与regex /\\w+|\\s+|[^\\s\\w]+/g

  1. \\w+ - 任何单词匹配
  2. \\s+ - 用于空格
  3. [^\\s\\w]+ - 用于匹配除空白和单词字符之外的任何内容的组合。

 var text = "I like grumpy cats. Do you?"; console.log( text.match(/\\w+|\\s+|[^\\s\\w]+/g) ) 

正则表达式在这里解释


仅供参考:如果您只想匹配单个特殊字符,则可以使用\\W. 而不是[^\\s\\w]+

边界\\b一词应该可以正常工作。

"I like grumpy cats. Do you?".split(/\b/)
// ["I", " ", "like", " ", "grumpy", " ", "cats", ". ", "Do", " ", "you", "?"]

编辑

处理案件. ,我们也可以在[.\\s]上拆分它

"I like grumpy cats. Do you?".split(/(?=[.\s]|\b)/)
// ["I", " ", "like", " ", "grumpy", " ", "cats", ".", " ", "Do", " ", "you", "?"]
  • (?=[.\\s]正向前看,在之前分裂.或者\\s
var text = "I like grumpy cats. Do you?"
var arr = text.split(/\s|\b/);
alert(arr);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM