Javascript regex获取所有匹配的数组,而不仅仅是第一次出现[duplicate]

时间:2022-03-19 04:53:12

This question already has an answer here:

这个问题已经有了答案:

I am trying to match all instances of either rel| or syn| and any word after it. I've got the correct regex as you can see here: https://regex101.com/r/E7RsbZ/1 but when I put it into my JS it only seems to match the first instance of rel| or syn|. Here's the relevant code:

我正在尝试匹配所有rel|或syn|的实例以及后面的任何单词。我得到了正确的regex,如您在这里看到的:https://regex101.com/r/E7RsbZ/1,但是当我将它放入我的JS中时,它似乎只匹配|或syn|的第一个实例。相关代码:

var regex = /(syn|rel)\|[a-zA-Z]*/mgi;

var regexed1 = regex.exec(text);

When text is the same as the regex101 input, it returns the array ['syn|felicitous', 'syn'].

当文本与regex101输入相同时,它会返回数组['syn|, it 'syn']。

I'm not sure why it's doing this. What I want it to return is an array that contains all instances of rel| or syn| so that I can .toString() it and print it out. I found this SO answer which implies I should use a loop, but the problem is I won't always know how many instances of rel| or syn| there will be, so I can't hard-code the number of times it should loop.

我不知道为什么会这样。我希望它返回的是一个包含所有rel|或syn|实例的数组,这样我就可以。tostring()将它打印出来。我发现这个答案意味着我应该使用一个循环,但问题是我不总是知道会有多少rel|或syn|实例,所以我不能硬编码它应该循环的次数。

Any help with this would be appreciated, not sure where to go.

如果有任何帮助,我们会很感激,不知道去哪里。

2 个解决方案

#1


3  

You're looking for String#match, which returns an array containing all matches when the regular expression passed has the global (g) flag:

您正在寻找String#match,它返回一个数组,该数组在正则表达式传递时包含所有匹配项,并带有全局(g)标志:

var string = '\
adjective|syn|felicitous\
adjective|syn|glad\
adjective|syn|well-chosen\
adjective|ant|unhappy\
adjective|rel|felicitous\
adjective|rel|glad\
adjective|rel|cheerful\
adjective|rel|content\
adjective|rel|contented\
adjective|rel|elated\
adjective|rel|euphoric\
adjective|rel|joyful\
adjective|rel|joyous\
adjective|sim|felicitous\
adjective|sim|blessed\
adjective|sim|blissful\
adjective|sim|bright\
adjective|sim|fortunate\
adjective|sim|golden\
adjective|sim|halcyon\
adjective|sim|laughing\
adjective|sim|prosperous\
adjective|sim|riant\
adjective|sim|willing\
'

var regex = /(syn|rel)\|[a-zA-Z]*/mgi;

var matches = string.match(regex)

console.log(matches)
.as-console-wrapper { min-height: 100vh; }

#2


1  

exec() Tests for a match in a string. Returns the first match.

exec()测试字符串中的匹配。返回第一个匹配。

You can use match() for returning the entire matched characters found.

可以使用match()返回找到的所有匹配字符。

The match() method retrieves the matches when matching a string against a regular expression.

match()方法在将字符串与正则表达式匹配时检索匹配。

Syntax:

语法:

str.match(regexp)

for more information see MDN

有关更多信息,请参见MDN

#1


3  

You're looking for String#match, which returns an array containing all matches when the regular expression passed has the global (g) flag:

您正在寻找String#match,它返回一个数组,该数组在正则表达式传递时包含所有匹配项,并带有全局(g)标志:

var string = '\
adjective|syn|felicitous\
adjective|syn|glad\
adjective|syn|well-chosen\
adjective|ant|unhappy\
adjective|rel|felicitous\
adjective|rel|glad\
adjective|rel|cheerful\
adjective|rel|content\
adjective|rel|contented\
adjective|rel|elated\
adjective|rel|euphoric\
adjective|rel|joyful\
adjective|rel|joyous\
adjective|sim|felicitous\
adjective|sim|blessed\
adjective|sim|blissful\
adjective|sim|bright\
adjective|sim|fortunate\
adjective|sim|golden\
adjective|sim|halcyon\
adjective|sim|laughing\
adjective|sim|prosperous\
adjective|sim|riant\
adjective|sim|willing\
'

var regex = /(syn|rel)\|[a-zA-Z]*/mgi;

var matches = string.match(regex)

console.log(matches)
.as-console-wrapper { min-height: 100vh; }

#2


1  

exec() Tests for a match in a string. Returns the first match.

exec()测试字符串中的匹配。返回第一个匹配。

You can use match() for returning the entire matched characters found.

可以使用match()返回找到的所有匹配字符。

The match() method retrieves the matches when matching a string against a regular expression.

match()方法在将字符串与正则表达式匹配时检索匹配。

Syntax:

语法:

str.match(regexp)

for more information see MDN

有关更多信息,请参见MDN