Today on the #codeigniter IRC channel someone asked about how to match a string that didn’t start with a specific word using a regex. I quickly threw out that, off the top of my head, /^(word){0}/ should work. Well, surprisingly, it didn’t. Turns out negatively matching words with regular expressions is a little more difficult.
After a little research I came up with a working solution: /^(?!word).*/
This post by Jeff Atwood helped: Excluding matches with Regular Expressions
Been fighting this same thing ALL day! Thank you so much for the post, I am glad I finally found it!
Hi, I’m trying to do something like that.
But it doesn’t work.
I need to write a regular expression that find in a string with HTML code tags INPUT that haven’t the STYLE attribute.
Something like this…
(?i:)
Thanks for help.
Sorry but this site isn’t prepare to take HTML code that a posted.
Francisco,
This regex seems to work for me:
/<input(?![^>]+style=)[^>]*>/
Thanks, It saves me some time..:)
Looking for a solution to match local links (staying on the same domain) and disregard all others. This seems to work:
/href=\”([^(\/\/)]+)\”/
OMG!
THANK YOU.
I was trying like crazy to negate:
^K[0-9]{2}\.[0-9].*$
And:
^(?!K[0-9]{2}\.[0-9]).*$
did the trick perfectly