How can we select specific regex group in k6?
In the below regex we have 4 groups, so how I need to select each group and assign it to a variable?
match = new RegExp(
"\\/\\/View Product event\\s+tracker\\.setEcommerceView\\('(.*?)', \\/\\/ \\(required\\) SKU: Product unique identifier\\s+'(.*?)', \\/\\/ \\(optional\\) Product name\\s+\\['(.*?)'\\], \\/\\/ \\(optional\\) Product category, or array of up to 5 categories\\s+'(.*?)'\\);"
vars["cp_productId"] = match ? match || match : null;
Hi @divinp ,
as per the Regexp.prototype.exec documentation the first index of the result is the whole match and then each next index is the corresponding group so in your case
match will be w/e the 4th group matched.
var match = new RegExp(
"aaaa(ab+) something ([^ ])+ here is a (third) and a (forth match .+) end."
).exec("aaaaaaabbbbb something else here is a third and a forth match with something until the end.");
console.log(JSON.stringify(match, null, " "));`
"aaaaabbbbb something else here is a third and a forth match with something until the end.",
"forth match with something until the"
Hope this helps you
@mstoykov Could you please let me know, how will I assign these matches to a variable?
The same way you will do that for anythign else
let variablename = match; // possibly with the `let ` if it's already defined
How can we set ordinal position in regex ?
@mstoykov could you please look into this
Can you expand on your question as I don’t understand what you are trying to do
@mstoykov : Lets say for a particular match there are 3 occurrence:
And I need to fetch the 3rd value or 3rd ordinal position, so how can I obtain that?
Along with this I would like to know how can we fetch a random value from all the occurrence?
You likely want to use matchAll.