CoopC
CoopC

Reputation: 140

Javascript regex with negative lookbehind (review)

I was hoping someone could review my regular expression to make sure that it's doing what I want it to do.

So here's what I'm after:

  1. Search for a word within a word boundary - so it can be a word on its own, or a word within another word
  2. Grab the preceding 30 characters (if preceding characters exist), but only if they do not contain the word I am searching for.
  3. Grab the next 30 characters (if they exist)

So if I were searching for "car" for 1, I have:

(\b\w*car\w*\b)

For 2, I have:

((?!\b\w*car\w*\b).{30}|.{0,30})

For 3, I have:

.{0,30}

All together:

((?!\b\w*car\w*\b).{30}|.{0,30})(\b\w*car\w*\b).{0,30}

Have I got it right, will this do what I'm after?

Upvotes: 2

Views: 53

Answers (1)

br3nt
br3nt

Reputation: 9606

There is a slight change that needed to be made to your regex.

The last .{0, 30} needs to be put into a group. The full regex is then becomes this:

/((?!\b\w*car\w*\b).{30}|.{0,30})(\b\w*car\w*\b)(.{0,30})/

If you run this in a javascript console you can see the output:

var str = "I'm an psychodelic purple and green electric smartcar that goes insanely really fast and can lap the Earth on a single charge"
var match = str.match(/((?!\b\w*car\w*\b).{30}|.{0,30})(\b\w*car\w*\b)(.{0,30})/);

alert(
  "Word containing car: " + match[2] +
  "\nFirst 30: " + match[1] +
  "\nLast 30: " + match[3]
);

You should get the following alert message:

Word containing car: smartcar
First 30: lic purple and green electric 
Last 30:  that goes insanely really fas

Upvotes: 1

Related Questions