Reputation: 1959
I have a string which has multiple keywords with values separated by a colon-character (and maybe a space). The keyword and value pairs are themselves only separated by a space (or nothing at all).
Some examples would be:
"TAG1: asd asd testTAG2:TEST"
"TAG1:asd asd test TAG2:TEST"
"TAG1: asd asd test TAG2:TEST TAG1:hello"
I'm then using this regex to match (TAG1|TAG2): ?(.+)
which works only if I have a single tag pair. So I tried to use positive lookahead ((TAG1|TAG2): ?(.+)(?=TAG1|TAG2)
) to match until the next occurance but this obviously does not work if I have a single pair, and it does not work if I have more than two pairs either.
So what I'm wondering is how you can split it into matches which then has a group for the tag-name and another group for the value:
"TAG1: asd asd testTAG2:TEST"
[["TAG1", "asd asd test"], ["TAG2", "TEST"]]
"TAG1:asd asd test TAG2:TEST"
[["TAG1", "asd asd test"], ["TAG2", "TEST"]]
"TAG1: asd asd test TAG2:TEST TAG1:hello"
[["TAG1", "asd asd test"], ["TAG2", "TEST"], ["TAG1", "hello"]]
What alternative to lookahead is there that doesn't include it in the match? I think that's the problem here since it then skips over the next tag.
Upvotes: 2
Views: 198
Reputation: 521239
We can try using a regex split twice, once to obtain an array of all tags and their values, and a second time to populate a 2D array.
var input = "TAG1: asd asd test TAG2:TEST TAG1:hello";
var tags = input.split(/\s*(?=TAG)/);
var output = [];
tags.forEach(function (item, index) {
output.push(item.split(/:\s*/));
});
console.log(output);
Upvotes: 1
Reputation: 91415
I'd use split method:
var test = [
"TAG1: asd asd testTAG2:TEST",
"TAG1:asd asd test TAG2:TEST",
"TAG1: asd asd test TAG2:TEST TAG1:hello"
];
console.log(test.map(function (a) {
return a.split(/(TAG1|TAG2):/);
}));
Upvotes: 1