如何提取组的所有匹配项
How to extract all matches of a group
希望有人能帮我解决我的问题。我正在尝试使用相同的模式在所有值字段的文本范围内捕获相同的组。
我尝试从 html 中捕获仅与 attribute_2 相关的每个 "value"。
这是我的方法:
https://regex101.com/r/z5T20g/2
<select id="product_attribute_1">
<option value="1">2.2 GHz</option>
<option value="2">2.5 GHz</option>
</select>
<select id="product_attribute_2">
<option value="3">2GB</option>
<option value="4">4GB</option>
<option value="5">8GB</option>
</select>
product_attribute_2[\s\S]*"(\d+)"[\s\S]*select> ---this show last match (5)
product_attribute_2[\s\S]*?"(\d+)"[\s\S]*select> --this first (3)
如何提取所有值 (3,4,5)?可能有不同数量的值。请帮助)
P.S。我没有尝试用正则表达式解析 html。此值用于 Gatling 脚本。
我使用这个例子来收集独特的属性。想想我的情况,我可以使用这样的东西。
\b(product_attribute_\d)(?![\s\S]*?\b)
我在 Gatling 工具 (Scala) 中使用正则表达式从响应主体中提取值。
这会将所有值保存到列表中。
通过 css-选择器
select[id='product_attribute_2'] [value]
您可以使用 vanilla JavaScript 提取值,如下所示:
const options = Array.from(document.querySelector('#product_attribute_2').children);
const values = options.map(x => Number(x.getAttribute('value')));
console.log(values);
<select id="product_attribute_1">
<option value="1">2.2 GHz</option>
<option value="2">2.5 GHz</option>
</select>
<select id="product_attribute_2">
<option value="3">2GB</option>
<option value="4">4GB</option>
<option value="5">8GB</option>
</select>
如果我们必须为这个问题应用正则表达式,我们可能会从这个简单的表达式开始:
.*product_attribute_2.*?|option value="(\d+)"|<\/select>.*
在 s
模式下:
Demo on S Mode
或者用这个表达式:
[\s\S]*product_attribute_2|option value="(\d+)"|<\/select>[\s\S]*
在 m
模式下:
Demo on M Mode
const regex = /.*product_attribute_2.*?|option value="(\d+)"|<\/select>.*/gs;
const str = `<select id="product_attribute_1">
<option value="1">2.2 GHz</option>
<option selected="selected" value="2">2.5 GHz</option>
</select>
<select id="product_attribute_2">
<option value="3">2GB</option>
<option value="4">4GB</option>
<option value="5">8GB</option>
</select>`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
希望有人能帮我解决我的问题。我正在尝试使用相同的模式在所有值字段的文本范围内捕获相同的组。
我尝试从 html 中捕获仅与 attribute_2 相关的每个 "value"。 这是我的方法: https://regex101.com/r/z5T20g/2
<select id="product_attribute_1">
<option value="1">2.2 GHz</option>
<option value="2">2.5 GHz</option>
</select>
<select id="product_attribute_2">
<option value="3">2GB</option>
<option value="4">4GB</option>
<option value="5">8GB</option>
</select>
product_attribute_2[\s\S]*"(\d+)"[\s\S]*select> ---this show last match (5)
product_attribute_2[\s\S]*?"(\d+)"[\s\S]*select> --this first (3)
如何提取所有值 (3,4,5)?可能有不同数量的值。请帮助)
P.S。我没有尝试用正则表达式解析 html。此值用于 Gatling 脚本。 我使用这个例子来收集独特的属性。想想我的情况,我可以使用这样的东西。
\b(product_attribute_\d)(?![\s\S]*?\b)
我在 Gatling 工具 (Scala) 中使用正则表达式从响应主体中提取值。
这会将所有值保存到列表中。
通过 css-选择器
select[id='product_attribute_2'] [value]
您可以使用 vanilla JavaScript 提取值,如下所示:
const options = Array.from(document.querySelector('#product_attribute_2').children);
const values = options.map(x => Number(x.getAttribute('value')));
console.log(values);
<select id="product_attribute_1">
<option value="1">2.2 GHz</option>
<option value="2">2.5 GHz</option>
</select>
<select id="product_attribute_2">
<option value="3">2GB</option>
<option value="4">4GB</option>
<option value="5">8GB</option>
</select>
如果我们必须为这个问题应用正则表达式,我们可能会从这个简单的表达式开始:
.*product_attribute_2.*?|option value="(\d+)"|<\/select>.*
在 s
模式下:
Demo on S Mode
或者用这个表达式:
[\s\S]*product_attribute_2|option value="(\d+)"|<\/select>[\s\S]*
在 m
模式下:
Demo on M Mode
const regex = /.*product_attribute_2.*?|option value="(\d+)"|<\/select>.*/gs;
const str = `<select id="product_attribute_1">
<option value="1">2.2 GHz</option>
<option selected="selected" value="2">2.5 GHz</option>
</select>
<select id="product_attribute_2">
<option value="3">2GB</option>
<option value="4">4GB</option>
<option value="5">8GB</option>
</select>`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}