JavaScript Regex: String Does Not Contain

By  on  

For some reason I think that regular expressions can solve every problem and for some reason I always find out the regular expression I'm trying to create is too complicated and probably not the best route to getting what I want.  Case in point:  I needed to create a regular expression which is meant to not match a string.  I don't want to match a string which includes [requires-login] but do want to match everything else.  Let's set aside the why for now, here's how I accomplished what I wanted:

// Adding extra forward slashes for "[" and "]"
var regex = '^(?!.*?\\[requires-login\\])';

// Compile
var re = new RegExp(regex);

So why do I want to do this?  Within my intern client side tests, I mark tests requiring login as [requires-login], and if a username and password aren't passed in via command line, I want to skip those tests.  In that case, I leverage intern's grep function to match tests without the given string.  I'd prefer to mark a few tests with [requires-login] instead of mark most of them as [no-login].

Sometimes you want to do things within the web development world which aren't ideal but you simply need to accomplish them within a given parameter set.  This is one of those cases.  In the end you need to make it happen, and in this case, I did so!

Recent Features

  • By
    5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

  • By
    MooTools 1.2 Image Protector: dwProtector

    Image protection is a hot topic on the net these days, and why shouldn't it be? If you spent two hours designing an awesome graphic, would you want it ripped of in matter of seconds? Hell no! That's why I've created an image...

Discussion

  1. I’m wondering why you couldn’t simply write a simpler expression to match

    [required-login]

    and then negate the match. e.g.

    
    
  2. ArugulaExpression

    Your example may be for an “un-ideal scenario”, but I can imagine many common situations that require matching one portion and then un-matching another portion.

    Thank you for this and all of your other short lessons.

    – fan

  3. MaxArt

    Just keep in mind that regular expressions are ususally quite slow, so for you case it may be better another approach. In this case, the classic use of indexOf may be better… and clearer, too. In fact, another con point is that regexes are way harder to understand and especially *maintain*.

    • I have to bend to Intern’s API, and that means using grep, which requires a regex. Otherwise I’d agree with you.

    • You “have to bend to”… sounds like a fail right there. What is so great about this Intern that makes it mandatory in your workflow? (Disclaimer: I know nothing about it.)

    • You could very easily argue that Intern allowing you to write a regex is the ultimate in configurability.

  4. I’ve used Regex Coach in the past but abandoned it for RegexBuddy. It’s not a free tool, but I think it is among the best on the market.

  5. Thanks David. As a side question, how do you mark the tests in intern?

  6. What does the comment about “extra forward slashes” mean? I can’t see any forward slashes.

    • Chirag

      I think he meant “backward slashes”

  7. Hi David,

    I completely agree that regular expressions can solve lots of problem especially when we write complex and sophisticated search programs and validation code. It makes life alot easier by reducing several lines of code into just one. But to apply it, you must learn it and learning it requires some effort, atleast basics should be clear.

  8. Gabriel

    Thanks David! This is just what I needed. Indeed I run into an awkward situation too and the only solution was to find out that regex.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!