Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does "(?!$)" inside a regexp mean?

In a section of Sevelte tutorial, there’s a piece of code like this:

view = pin ? pin.replace(/\d(?!$)/g, '•') : 'enter your pin';

I know \d means a digit, but can’t figure out what (?!$) means.

(And because it’s composed of all punctuation, I can’t manage to google for an explanation.)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Please help, thanks.

>Solution :

  • \d matches all digits
  • ?! means ‘Negative Lookahead’
  • $ matches the end of a string

So when \d(?!$) is used, it matches all digits before the last character

In this string:

$$//www12.example@news.com<~>998123000dasas00–987

This will be matched (7 will not because it is the last character):

129981230000098

Referred to this answer
and Regex Cheat Sheet

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading