Say for example I have this text:
hello world **ant*** lorem **cat** opposum** *** ***antelope*** *rabbit __dog__
I would like to match strings that only have **
and __
as its preceding and concluding characters. So in the case above, the matches that I would only want are "cat" and "dog". This means that I have to cancel or negate the match if there are extra surrounding characters. For example, ***dog**
or __dog___
should fail.
I've tried to solve this using a negative look around http://www.regular-expressions.info/lookaround.html to no avail.
Here's the current pattern I have
const pattern = /([^*])*(w+)*([^*])/g;
const match = pattern.exec(text);
const annotatedText = match[0];
const matchedText = match[1];
// Return if annotatedText is a possible match for bolditalic
if (annotatedText.startsWith("***") || annotatedText.startsWith("___")) {
return;
}
// Return if the matchedText has spaces in between
if (/s/.test(matchedText)) {
return;
}
if (text.match(/^([*_
]+)$/g)) {
return;
}
in javascript regex,
Essentially, I want to remove the javascript string checks and add the logic on the regex pattern itself.
question from:
https://stackoverflow.com/questions/65910952/regex-negation-handling-conditional-if-statements-that-cancel-the-match-if-fulf 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…