Skip to content Skip to sidebar Skip to footer

How Match A Regex If It Does Not Contain A Particular Word?

I want to write a regex in Python or JavaScript to match if the given string does not JUST contain the given word (e.g 'any'). For example : any : does not match AnY : does

Solution 1:

If you also need words other that starting with "any" you can use a negative lookahead

^(?!any$).*$

This will match anything besides "any".

Solution 2:

It is probably more efficient to not use a regex, this works as well:

def noAny(i):
    i = i.lower().replace('any', '')
    return len(i) > 0

Solution 3:

Something like this:

/(any)(.+)/i

Solution 4:

any.+

..and some text to make the 30char threshold

Solution 5:

Use string.match(regexp) method in javascript for this purpose. See the code below:

<scripttype="text/javascript">var str="source string contains YourWord"; 
      var patt1=/YourWord/gi; // replace YourWord within this regex with the word you want to check.  if(str.match(patt1))
      {
         //This means there is "YourWord" in the source string str. Do the required logic accordingly.
      }
      else
      {
          // no match
      }
</script>

Hope this helps...

Post a Comment for "How Match A Regex If It Does Not Contain A Particular Word?"