Regular Expressions: Using A Negative Look Ahead For The Nonsupported Negative Look Behind And Capturing The Look Behind Characters Upon Split
I'm struggling again with regular expressions. I've been trying to add the use of an escape character to escape a custom tag such as <1> to <57> and to
Solution 1:
You are splitting on desired sub-strings and use a capturing group to have them in output. This could be happened about undesired sub-strings too. You match them and enclose them in a capturing group to have them in output. The regex would be:
(undesired-part|desired-part)
Regex for undesired sub-strings should come first because desired ones could be found in them i.e. <21>
is included in \<21>
so we should match the latter earlier.
You wrote the desired part and it is known to us:
(undesired-part|<\/?(?:[1-9]|[1-4]\d|5[0-7])>)
So what about undesired? Here it is:
(?:[^<\\]+|\\.?|<(?!\/?(?:[1-9]|[1-4]\d|5[0-7])>))+
Let's break it down:
(?:
Start of non-capturing group[^<\\]+
Match anything except<
and\
|
Or\\.?
Match an escaped character|
Or<(?!\/?(?:[1-9]|[1-4]\d|5[0-7])>)
Match a<
which is not desired
)+
End of NCG, repeat as much as possible and at least once
Overall it is:
((?:[^<\\]+|\\.?|<(?!\/?(?:[1-9]|[1-4]\d|5[0-7])>))+|<\/?(?:[1-9]|[1-4]\d|5[0-7])>)
Js code:
console.log(
'This is a \\<21>test</21> ag<ain\\.'.split(/((?:[^<\\]+|\\.?|<(?!\/?(?:[1-9]|[1-4]\d|5[0-7])>))+|<\/?(?:[1-9]|[1-4]\d|5[0-7])>)/).filter(Boolean)
);
Post a Comment for "Regular Expressions: Using A Negative Look Ahead For The Nonsupported Negative Look Behind And Capturing The Look Behind Characters Upon Split"