Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    43
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Regex for string not containing a word

    Please suggest that how can I write a regex where I want to match a string "sourcecode_notContainWordblah_followed by anything"

    So basically the string should contain sourcecode and can be followed by any character or word but should not contain "blah" just after sourcecode.

    Thanks.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Code:
    var str = "sourcecode_notContainWordblah_followed by anything";
    var x = str.match(/sourcecode(?!.*blah)/gi);  // i.e.sourcecode if not followed by blah
    alert (x);  // sourcecode if blah not present, null if blah present

    It's quite hard to know who to nail the colours of your mast to. - Commentator Radio 4
    Last edited by Philip M; 07-31-2013 at 08:44 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •