I am working on a Node.js project, in this project we are searching a bunch of PHP view files, and replacing some of the attributes. I am trying to get the HTML open tag attribute values, and replace them.
Basically, if this is the tag
<tag attr1="[capture ANYTHING inside single/double qoutes]" attr2='[CAPTURE ANYTHING]'></tag>
I want to capture anything inside the attribute quotes.
and by [ANYTHING]
I mean really anything!
example2: attr="with HTML <br/><b>also been captured</b>"
example3: attr="with line break style \n or \n\r
this is still is part of what should been captured
and this line too!"
example4: attr="a PHP code <?php echo $ThisPHPcodeisInsideTheQoutes?> should be captured as well!"
example5: title="{{angular?'if inside the attribute': 'it should be acptured as well' }}"
I had wrote the next regex:
/<\w+\s+(:?[\w-]+=(:?"|')(.|[\r\n])*?\2\s*?)>?/g
this regex is catching only the first attribute.
Here is a fiddle with some demo data
regex breakdown:
<
tag start\w+
a word, mainly tag name this will force avoiding PHP tags<?php
\s+
a space or multiple sapces<tag attr
(:?
a non capturing group1, I want to get Multiple attributes, but capture only the content![\w-]+
a word or-
for exampleattr
orng-attr
=
the attribute equal sign(:?"|')
a non capturing group2 open quote or double qoutes(.|[\r\n])*?
-- the actual data I am trying to capture, capture everything.
or[\r\n]
line break\2
- back reference to(:?"|')
so well have "[data]" or '[data]'\s*?
- zero or more sapces before the next tag not greedy)
- close of non capturing group1>?
- end of opening tag not greedy
I don't understand why multiple attributes are not being captured Thanks in advance for the help
(:?
is a non-capturing group?\w
will match the?
in<?php
? Are you not allowing spaces before and after the=
? How are you trying to use this regexp (show code)?>?
is a non-greedy match (hint: no, it's an optional>
). - user663031(:?
is not a non-capturing group; it's a group starting with an optional:
. You probably meant(?:
. This could possibly be the reason for your regexp not capturing multiple attributes. - user663031\2
supposed to refer to, since you're (trying) not to capture the group containing the quotes, right? - user663031(:?
, which, as I said an hour ago, is NOT a non-capturing group, but rather a capturing group starting with an optional colon. If you love the regexp editors so much, please review CAREFULLY their narrative description of your(:?
construct. - user663031