2
I’m trying to capture a certain structured data, and I need it to capture a certain group as long as there are possibilities.
The data format is as follows:
foo01@chave1|valor1#chaveN|valorN
Where the first group is composed of a numerical alpha value, separating the other groups with the character @
, my interest is to obtain only the foo01
in the first group.
The second group is what is repeated, where Attribute and value are separated by |
and the other attributes are separated by #
, after that there is no more information only attributes in the format "Chave1|value1#chaveN|valueN".
Below is possible to see what I started to do, but I could not capture all attributes separately.
const regex = /(^[a-zA-Z0-9_]*@)([a-zA-Z0-9_]*\|[a-zA-Z0-9_]*)/g;
const str = `foo01@chave1|valor1#chaveN|valorN`;
let m;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
m.forEach((match, groupIndex) => {
console.log(`Encontrado, grupo ${groupIndex}: ${match}`);
});
}
I would like to know how to capture all occurrences of "Chave1|value1#chaveN|valueN" inside a string independent of the quantity present in the string.
In case, do you need to capture all attributes separately independent of the quantity present in the string? I got it right?
– Woss
Exactly, I need the group
([a-zA-Z0-9_]*\|[a-zA-Z0-9_]*)
be captured n times until the end. I’m worried about performance here, I could do it using interaction, but I believe regular expression will be much more performatic.– LeonanCarvalho
I’m not sure if regex supports something like this. I remember once searching for a similar solution and finding nothing. I had to call the JS anyway.
– Woss
Good question +1
– Paz