简体   繁体   English

从字符串中取出单词并转换为数组

[英]Take words from string and convert to array

Seems simple enough but I can't find an answer on Stack that has a good solution. 看起来很简单,但我无法在Stack上找到一个有良好解决方案的答案。 Feel free to point me in the right direction. 随意指出我正确的方向。

The regex should allow me to do a javascript split to convert a string into an array. 正则表达式应该允许我做一个javascript split将字符串转换为数组。 The basic test is that: 基本测试是:

test1 test2, tes-t3; t"e's-----4.      test5

should be split into an array that contains: 应该拆分成一个包含以下内容的数组:

[test1, test2, tes-t3, t"e's-----4, test5]

Best way to achieve this? 实现这一目标的最佳方式?

Use String.split(/[\\s,;.]+/) : 使用String.split(/[\\s,;.]+/)

var s = 'test1 test2, tes-t3; t"e\'s-----4.      test5';
s.split(/[\s,;.]+/)
=> ["test1", "test2", "tes-t3", "t"e's-----4", "test5"]

or String.match(/[-'"\\w]+/g) : String.match(/[-'"\\w]+/g)

s.match(/[-'"\w]+/g)
=> ["test1", "test2", "tes-t3", "t"e's-----4", "test5"]

try this also, "test1 test2, tes-t3; t\\"e's-----4. 试试这个,“test1 test2,tes-t3; t \\”e是----- 4。 test5".split(/\\s+|[,.;]\\s*/); TEST5" .split(/ \\ S + | [,;。] \\ S * /);

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

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