Reputation: 29
Hope someone could help me with my problem. I am trying to use same pattern for capturing same group in a range of text for all values fields.
I try to capture every "value" only related for attribute_2 from the html. This is my approcah: 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)
How can I extract all values (3,4,5)? There is could be different amount of values. Help please )
P.S. I am not trying to parse html with regex. This values is used in Gatling script. I was using this example for collecting unique attributes. Think in my case I can use something like this.
\b(product_attribute_\d)(?![\s\S]*?\b\1)
I use regex for extracting values from response body in Gatling tool (Scala).
This will save all values to List.
Make it through css-selector
select[id='product_attribute_2'] [value]
Upvotes: 0
Views: 326
Reputation: 27723
If we have to apply a regular expression for this problem, we can likely start with this simple expression:
.*product_attribute_2.*?|option value="(\d+)"|<\/select>.*
on s
mode:
or with this expression:
[\s\S]*product_attribute_2|option value="(\d+)"|<\/select>[\s\S]*
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}`);
});
}
Upvotes: 2
Reputation: 5853
You can extract the values using vanilla JavaScript as follows:
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>
Upvotes: 0