Reputation: 140
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:
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
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