I'm having a hard time with vscode's oniguruma regex parsing for TextMate. Apparently you can't use a newline inside a lookahead, even though oniguruma actually supports it, it's probably not enabled in vscode's version of oniguruma.
I need to match the beginning of a string if, and only if, after element there is desiredAttr1="desiredValue1" or desiredAttr2="desiredValue2":
<element attribute="value" desiredAttr1="desiredValue1" desiredAttr2="desiredValue2">
So far so good, but the thing is, these attributes can be in any order, and there can be a newline in between them. Eg.:
<!-- Should match -->
<element
attribute="value"
desiredAttr1="desiredValue1"
desiredAttr2="desiredValue2"
>
<!-- Should match -->
<element
attribute="value"
desiredAttr2="desiredValue2"
>
<!-- Should match -->
<element attribute="value" desiredAttr1="desiredValue1">
<!-- Should match -->
<element desiredAttr2="desiredValue2" attribute="value">
<!-- Should NOT match -->
<element
attribute="value"
notDesiredAttr1="desiredValue1"
notDesiredAttr2="desiredValue2"
>
This is what I got so far (and it works on rubular):
/(^[\t]+)?(?=<(?i:element)\b(?!-)[\s\w\W]*(?:((desiredAttr1="desiredValue1")|(desiredAttr2="desiredAttr2"))))/
Note: I tried also replacing \s with [:space:] and [^/]
This is what I need to match:
<span style="background: red;"> </span><code><element<br/>
attribute="value"<br/>
desiredAttr1="desiredValue1"<br/>
desiredAttr2="desiredValue2"<br/>
></code>
Is there any other alternative I could use? Thanks in advance.
Assuming there are no angle brackets in between, you could use:
The pattern matches:
^Start of string[\p{Zs}\t]*Match optional spaces or tabs(?=Positive lookahead<element\bMatchelementfollowed by a word boundary[^<>\r]*Optionally repeat matching any char except<>or\r\bdesiredAttr([12])=matchdesiredAttrand capture either 1 or 2 in group 1"desiredValue\1"match"desiredValue\1"where\1is a backreference to the captured digit in group 1 (to match the same digit)[^<>\r]*Optionally repeat matching any char except<>or\r>Match literally)Close the lookaheadSee a regex demo.