How can I match all the strings, in any order, but only if preceded by certain key?
example I want to match article and legal no matter the order as long as they are preceded by tags:
---
author: Karen the Trollmaster
tags: noise, legal, irrelevant, article
keywords: pff, nobody, likes, "a Karen", "not this", article // < don't match this one
---
What I have so far is
(?<=(tags: ))(article)|(legal)
but this isn't working correctly.
CodePudding user response:
I would do this in two steps, first isolate the tags:
line, then use match()
to find all keywords with an alternation regex.
var input = `---
author: Karen the Trollmaster
tags: noise, legal, irrelevant, article
keywords: pff, nobody, likes, "a Karen", "not this", article // < don't match this one
---`;
var keywords = ["article", "legal"];
var regexp = new RegExp("\\b(" keywords.join("|") ")\\b", "g");
var matches = input.match(/\btags:.*?(?=\n|$)/)[0].match(regexp);
console.log(matches);
CodePudding user response:
You can write the pattern as:
(?<=tags: .*)\b(?:article|legal)\b
The pattern matches:
(?<=
Positive lookbehind, assert to the left from the current positiontags: .*
Matchtags:
and 0 times any
)
Close lookbehind\b(?:article|legal)\b
Match either article or legal between word boundaries
Note that you can omit the capture groups for a match only
const regex = /(?<=tags: .*)\b(?:article|legal)\b/gm;
const str = `---
author: Karen the Trollmaster
tags: noise, legal, irrelevant, article
keywords: pff, nobody, likes, "a Karen", "not this", article // < don't match this one
---`;
console.log(str.match(regex));
If both words should be present, you can make use of lookarounds to match either of the words and assert the other word to be present on the left or the right side
(?<=tags: .*)(?:\blegal\b(?=.*\barticle\b)|\barticle\b(?=.*\blegal\b)|(?<=.*\barticle\b.*)\blegal\b|(?<=.*\blegal\b.*)\barticle\b)