2

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?

CoopC
  • 105
  • 1
  • 8
  • 2
    this http://codereview.stackexchange.com/ might be a better place for this question – depperm Dec 12 '15 at 04:54
  • 1
    Can you add some examples of valid and invalid strings for this expression? – Musa Haidari Dec 12 '15 at 04:57
  • Run this in a the console and check if the output is what you want/expect: `"I'm an psychodelic purple and green electric smartcar that goes insanely really fast and can lap the Earth on a single charge".match(/((?!\b\w*car\w*\b).{30}|.{0,30})(\b\w*car\w*\b)(.{0,30})/)` – br3nt Dec 12 '15 at 05:05
  • *Have I got it right, will this do what I'm after?* I don't know, does it? –  Dec 12 '15 at 05:12
  • Almost, slight change was required – br3nt Dec 12 '15 at 05:14
  • This appears to be an attempt at a workaround to JavaScript's lack of lookbehind support. In that light, perhaps you'd like to read more about [how to properly implement lookbehind in JavaScript](https://stackoverflow.com/questions/35142364/regex-negative-lookbehind-not-valid-in-javascript/35143111#35143111). – Adam Katz Feb 09 '16 at 01:36

1 Answers1

1

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
br3nt
  • 9,017
  • 3
  • 42
  • 63