1

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.

1
  • 1
    You should have updated your last question to makes it clearer, not post another very similar one! Commented Jan 21, 2011 at 7:09

3 Answers 3

7

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.