![](/img/trans.png)
[英]How to join the string either with a dot, exclamation mark, or a question mark?
[英]Javascript Regex Split Words that ends with dot, exclamation, question mark, comma and on whitespace
我需要在以点、感叹号/问号、逗号和空格结尾的单词上拆分字符串。
例如:
var s = "An example, string, That have weird? formatting!"
在s.split()
将导致:
["An", "example", ",", "string", ",", "That", "have", "weird", "?", "formatting", "?"]
您可能应该使用 match() 和分组来获得您想要的东西,而不是使用拆分。 基本思想是匹配一个单词或匹配特殊字符。
var str = "An example, string, That have weird? formatting!" var result = str.match(/(\\w+|[,?!])/g) console.log(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.