Click here to Skip to main content
16,016,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
What I am trying to do is modify the regular expression of:
JavaScript
[0-9]{3}( |-|)[0-9]{2}( |-|)[0-9]{4}(?![0-9])
to ignore strings that start with HSF.

What I have tried:

The best I can come up with is:

JavaScript
( |-|^|[^HSF])[0-9]{3}( |-|)[0-9]{2}( |-|)[0-9]{4}(?![0-9])
here are the results:

123121234 <- works fine, and is found by the regex

lksjfd 123121234 lksjdf <- works fine, and is found by the regex

hello world123121234bleh bleh bleh <- This is what is causing me concern. The substring of d123 is getting caught up in the regex but I do not want it.

you are a ding dong. HSF123121234 <- This is a sample string that I do NOT want to get caught by the regex.
Posted
Updated 14-Dec-22 5:46am

Depending on the browsers you need to support, a negative lookbehind seems to work:
RegEx
(?<!HSF)[0-9]{3}( |-|)[0-9]{2}( |-|)[0-9]{4}(?![0-9])
Demo[^]
https://javascript.info/regexp-lookahead-lookbehind[^]
Assertions - JavaScript | MDN[^]
 
Share this answer
 
To add to what Richard has said, if you are going to use Regular Expressions, then get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900