Eldar Khaitov Eldar Khaitov - 1 month ago 7
Node.js Question

Regexp match smth like <a href="..."> javascript

I managed to match

<a href="http://localhost:3000/#/project/57fa46a56fd91a88201943dc">
with
<a href[^\>]*>
.

But from server comes secured information with omitted parenthesis, e.g.
&lt;a href="http://localhost:3000/#/project/57fa46a56fd91a88201943dc"&gt;
. I cannot just write
&lt;a href[^\&gt;]*&gt;
because it reacts on any charachter
&
,
g
,
t
, or
;
in
href
attribute.

I can find information about matching words of one character after another but for some reason it doesn't work in negation
[^\(&gt;)]
.

So, what is the way to manage it so the code look fine?

Answer

The best fix is probably one that fixes your server so it returns your HTML a tag with correct < and > characters, rather than the encoded &lt; &gt;.

If that's not a possibility, try:

<|(\&lt)a href... which should match < or the literal &lt (& must be escaped, lt do not need to be escaped).

Check it works on regex101.com

Comments