-2

regex pattern(javascript and pcre)

<a href="(?<!#).*".*>(.*)<\/a>

This pattern should not select any html anchor tag of which the href attribute starts with a # symbol. But it matches the following code

<a href="#team">Team</a>

What am I doing wrong here?

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
KMA Badshah
  • 691
  • 5
  • 16
  • https://regex101.com/r/HBvG3K/16 - Does this work for you ? You can add closing anchor at end if you want. `^(?!\ – rootkonda Aug 13 '20 at 18:42

1 Answers1

0

Look-arounds are zero-width, meaning they don't consume any characters, making them only useful at the start and end of a pattern. #team is not preceded by #, so the first .* matches #team.

The way to write what you want is "[^#].*". This means the first character in the quotes must not be #. One caveat here is that it will not match empty strings, but that's easy enough to add like so: "([^#].*)?".

Zach Peltzer
  • 152
  • 3