Eduard Jacko
Eduard Jacko

Reputation: 2151

RegExp ignore opening characters if next closing not found

Is there a way how RegExp ignores a match if contains certain characters? I look around and found negative matches which works fine for certain cases (?!\<). But I can't find a way how to create a negative match with optional any character until next opening bracket found. I Apologies for poor explanation. Here is an example which makes make more sense I guess.

const text = "hello <friend>. My age is < twenty <unit>.>Signature<<signature>."
const matches = text.split(/(\<(?!\<).*?\>)/g)
// ["hello ", "<friend>", ". My age is ", "< twenty <unit>", ".>Signature<", "<signature>", "."]

The Problem is "< twenty <unit>". What I really looking for is two elements from value "< twenty ", "<unit>"

Upvotes: 0

Views: 165

Answers (1)

Onno Rouast
Onno Rouast

Reputation: 672

make the negative lookahead (?!) part of the . match. /(<((?!<)[^>])*>)/

This way every time * the dot is matched it has to comply with the look.

A lookahead works better than a lookbehind since the character you care about not matching is the next character, not the previous.

Also, replacing the anything match . with [^>] = everything except closing tag prevents overshooting the closing tag.

Instead of the lookahead you could also rewrite it to /(<[^><]*>)/, = match everything between opening and closing tag that does not contain any tag

I just reread your question. If I understand it correctly and you want to match the outer tag too, until the start of the inner tag, you can use this. It matches opening < + any non-tag + closing > if it exists

<[^><]*>?

Upvotes: 1

Related Questions