-1

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
  • 607,720
  • 39
  • 448
  • 563
KMA Badshah
  • 895
  • 8
  • 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

1

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
  • 172
  • 3