Reputation: 7400
In javascript, I want extract word list ends with 'y'.
code is following,
var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
str.match(/(\w+)y\W/g);
result is a array
["simply ", "dummy ", "industry.", "industry'", "dummy ", "galley ", "only ", "essentially ", "recently "]
so, my question is, Can I get a word list without 'y' character using regex. the result word list should be like this,
["simpl ", "dumm ", "industr.", "industr'", "dumm ", "galle ", "onl ", "essentiall", "recentl"]
/(\w+)y\W/g
doesn't work.
Upvotes: 1
Views: 17555
Reputation: 9348
You need what's called a look-ahead assertion: the (?=x)
means the characters in front of this match must match x
, but don't capture them.
var trimmedWords = wordString.match(/\b\w+(?=y\b)/g);
Upvotes: 7
Reputation: 15172
Here is a way to do it:
var a = [], x;
while (x = /(\w+)y\W/g.exec(str)) {
a.push(x[1]);
}
console.log(a);
//logs
["simpl", "dumm", "industr", "industr", "dumm", "galle", "onl", "essentiall", "recentl"]
Upvotes: 1
Reputation: 101614
I think you're looking for \b(\w)*y\b
. The \b is a word separator. The \w will match any word character, and the y to specify it's ending character. Then you grab the \w and exclude the y.
*EDIT I semi-take that back. If you're looking for "industr." (with the period included) this will not work. but I'll play around and see what I can come up with.
Upvotes: 1