gpt4 book ai didi

JavaScript 正则表达式和捕获组

转载 作者:行者123 更新时间:2023-11-28 15:16:38 28 4
gpt4 key购买 nike

我对 JavaScript 中的正则表达式不熟悉,并且无法从文本字符串中获取匹配数组,如下所示:

Sentence would go here
-foo
-bar
Another sentence would go here
-baz
-bat

我想获得像这样的匹配数组:

match[0] = [
'foo',
'bar'
]
match[1] = [
'baz',
'bat'
]

总而言之,我正在寻找的是:

任何出现在句子之后之后的破折号+单词(-foo、-bar等)

任何人都可以提供一个可以捕获所有迭代而不是最后一次迭代的公式,因为重复捕获组显然只会捕获最后一次迭代。如果这是一个愚蠢的问题,请原谅我。如果有人想给我发送一些测试,我正在使用 regex101

最佳答案

我想出的第一个正则表达式如下:

/([^-]+)(-\w*)/g

第一组([^-]+)抓取除破折号之外的所有内容。然后我们跟踪我们想要的实际捕获组(-\w+)。我们添加标志 g 以使正则表达式对象跟踪它最后查看的位置。这意味着,每次运行 regex.exec(search) 时,我们都会得到您在 regex101 中看到的下一个匹配项。

注意:JavaScript\w 相当于 [a-zA-Z0-9_]。因此,如果您只想要字母,请使用此代替 \w:[a-zA-Z]

<小时/>

这是实现此正则表达式的代码。

<p id = "input">
Sentence would go here
-foo
-bar
Another sentence would go here
-baz
-bat
</p>

<p id = "output">

</p>

<script>
// Needed in order to make sure did not get a sentence.
function check_for_word(search) {return search.split(/\w/).length > 1}
function capture(regex, search) {
var
// The initial match.
match = regex.exec(search),
// Stores all of the results from the search.
result = [],
// Used to gather results.
gather;
while(match) {
// Create something empty.
gather = [];
// Push onto the gather.
gather.push(match[2]);
// Get the next match.
match = regex.exec(search);
// While we have more dashes...
while(match && !check_for_word(match[1])) {
// Push result on!
gather.push(match[2]);
// Get the next match to be checked.
match = regex.exec(search);
};
// Push what was gathered onto the result.
result.push(gather);
}
// Hand back the result.
return result;
};
var output = capture(/([^-]+)(-\w+)/g, document.getElementById("input").innerHTML);
document.getElementById("output").innerHTML = JSON.stringify(output);
</script>
<小时/>

使用稍微修改过的正则表达式,您可能会得到更多您正在寻找的内容。

/[^-]+((?:-\w+[^-\w]*)+)/g

[^-\w]* 的额外位允许每个破折号单词之间存在某种分隔。然后添加非捕获组 (?:) 以允许 + 一个或多个破折号。我们也不需要 [^-]+ 周围的 (),因为不再需要数据,如下所示。第一个对于破折号之间可以分隔的内容更灵活,但我发现这个更清晰。

function capture(regex, search) {
var
// The initial match.
match = regex.exec(search),
// Stores all of the results from the search.
result = [],
// Used to gather results.
gather;
while(match) {
// Create something empty.
gather = [];

// Break up the large match.
var temp = match[1].split('-');
for(var i in temp)
{
temp[i] = temp[i].split(/\W*/).join("");
// Makes sure there was actually something to gather.
if(temp[i].length > 0)
gather.push("-" + temp[i]);
}

// Push what was gathered onto the result.
result.push(gather);

// Get the next match.
match = regex.exec(search);
};
// Hand back the result.
return result;
};
var output = capture(/[^-]+((?:-\w+[^-\w]*)+)/g, document.getElementById("input").innerHTML);
document.getElementById("output").innerHTML = JSON.stringify(output);
<p id = "input">
Sentence would go here
-foo
-bar
Another sentence would go here
-baz
-bat
My very own sentence!
-get
-all
-of
-these!
</p>

<p id = "output">

</p>

关于JavaScript 正则表达式和捕获组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33621271/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com