使用lodash _.words时如何在短划线/连字符上分开?

时间:2021-07-21 16:49:37

I am using lodash to split up usernames that are fed to me in a string with some sort of arbitrary separator. I would like to use _.words() to split strings up into words, except for hyphens, as some of the user names contain hyphens.

我正在使用lodash来分割用一些任意分隔符在字符串中提供给我的用户名。我想使用_.words()将字符串拆分为单词,但连字符除外,因为某些用户名包含连字符。

Example:

_.words(['user1,user2,user3-adm'], RegExp)

I want it to yield:

我想让它屈服:

['user1', 'user2', 'user3-adm']

not this (_.words(array) without any pattern):

不是这个(没有任何模式的_.words(数组)):

['user1', 'user2', 'user3', 'adm']

What is the right String/RegExp to use to make this happen?

什么是正确的String / RegExp来实现这一目标?

2 个解决方案

#1


5  

The initial case can be solved by this:

最初的案例可以通过以下方式解决:

_.words(['user1,user2,user3-adm'], /[^,]+/g);

Result:

["user1", "user2", "user3-adm"]

[EDITED]

If you want to add more separators, add like this:

如果要添加更多分隔符,请添加如下:

_.words(['user1,user2,user3-adm.user4;user5 user7'], /[^,.\s;]+/g);

Result:

["user1", "user2", "user3-adm", "user4", "user5", "user7"]

Last snippet will separate by:

最后一个片段将分开:

commas (,), dots (.), spaces (\s), semicolons (;)

逗号(,),点(。),空格(\ s),分号(;)


Alternatively you can use:

或者你可以使用:

 _.words(['user1,user2,user3-adm.user4;user5 user7*user8'], /[-\w]+/g)

Result:

 ["user1", "user2", "user3-adm", "user4", "user5", "user7", "user8"]

In this case you can add what you don't want as delimiter. Here it will will be separated by every character which is not \w (same as [_a-zA-Z0-9]) or -(dash)

在这种情况下,您可以添加您不想要的分隔符。在这里它将被每个不是\ w的字符分开(与[_a-zA-Z0-9]相同)或 - (破折号)

#2


4  

words accept a regex expression to match the words and not to split them, being so, just use a regex that matches everything besides a comma, i.e.:

单词接受正则表达式来匹配单词而不是分割它们,因此,只需使用匹配除逗号之外的所有内容的正则表达式,即:

_.words(['user1,user2,user3-adm'], /[^,]+/g);

Alternatively, you can use split.

或者,您可以使用拆分。

result = wordlist.split(/,/);

https://lodash.com/docs#words

#1


5  

The initial case can be solved by this:

最初的案例可以通过以下方式解决:

_.words(['user1,user2,user3-adm'], /[^,]+/g);

Result:

["user1", "user2", "user3-adm"]

[EDITED]

If you want to add more separators, add like this:

如果要添加更多分隔符,请添加如下:

_.words(['user1,user2,user3-adm.user4;user5 user7'], /[^,.\s;]+/g);

Result:

["user1", "user2", "user3-adm", "user4", "user5", "user7"]

Last snippet will separate by:

最后一个片段将分开:

commas (,), dots (.), spaces (\s), semicolons (;)

逗号(,),点(。),空格(\ s),分号(;)


Alternatively you can use:

或者你可以使用:

 _.words(['user1,user2,user3-adm.user4;user5 user7*user8'], /[-\w]+/g)

Result:

 ["user1", "user2", "user3-adm", "user4", "user5", "user7", "user8"]

In this case you can add what you don't want as delimiter. Here it will will be separated by every character which is not \w (same as [_a-zA-Z0-9]) or -(dash)

在这种情况下,您可以添加您不想要的分隔符。在这里它将被每个不是\ w的字符分开(与[_a-zA-Z0-9]相同)或 - (破折号)

#2


4  

words accept a regex expression to match the words and not to split them, being so, just use a regex that matches everything besides a comma, i.e.:

单词接受正则表达式来匹配单词而不是分割它们,因此,只需使用匹配除逗号之外的所有内容的正则表达式,即:

_.words(['user1,user2,user3-adm'], /[^,]+/g);

Alternatively, you can use split.

或者,您可以使用拆分。

result = wordlist.split(/,/);

https://lodash.com/docs#words