Negative Word Matching with Regular Expressions

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, /^(abc){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 and some trial and error, I came up with a working solution: /^(?!word).*/ This post by Jeff Atwood helped: Excluding matches with Regular Expressions

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*
-->