Andrew
Andrew

Reputation: 3570

Select all words that are not a specific word, via Javascript regex?

I want to select every word except the word: 'Lorem'.

I have tried:

^((?!Lorem).*)$ //nothing is selected.
^((?!test).*)$ //entire paragraph is selected.

Text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ut est quis felis volutpat pretium in et libero. Curabitur vulputate justo a tellus scelerisque viverra. Morbi ac leo nec dui feugiat imperdiet. Nam lacus augue, scelerisque at condimentum et, accumsan non nulla. Sed mi enim, iaculis ut placerat vitae, ullamcorper ac turpis. Phasellus erat velit, tristique eget cursus non, porttitor in purus. Integer pellentesque ultricies felis, non sodales enim bibendum nec. Nulla gravida ipsum sit amet ipsum porttitor dictum. Fusce mattis ultricies enim ut lORem adipiscing. In fermentum tellus sit lorem urna vestibulum fringilla id sit amet odio. Nunc eget lorem at leo commodo porta feugiat vel quam. Nam vehicula blandit neque, eget pharetra nulla mattis in. Nullam augue neque, lacinia quis commodo a, lobortis ut tellus. Curabitur eu tristique risus. Phasellus iaculis, sem sit amet aliquam tincidunt, odio ligula posuere mauris, sollicitudin pharetra massa tortor in leo. In interdum facilisis auctor. Nullam non congue felis. Vestibulum cursus ante a sapien commodo at luctus libero dapibus. Sed pulvinar aliquam nulla, tempor facilisis purus faucibus at.

Tested at: http://regexpal.com/

Upvotes: 2

Views: 844

Answers (4)

RobG
RobG

Reputation: 147503

No regular expression required:

s.split('Lorem').join('');

Of course you'll need one for a non–case sensitive version:

s.split(/Lorem/i).join('');

If by "select" you mean you want an array of every word other than lorem, take the output of the above and split on a word boundary, say \s+ or \b.

Upvotes: 0

ruakh
ruakh

Reputation: 183544

Are you saying that you want to create a copy of your paragraph, with the offending word removed? For that, you would use:

var filtered = paragraph.replace(/\bLorem\b/g, '');

Update based on comment below: Oh, so you want:

var filtered = paragraph.replace(/\b(?!Lorem\b)(\w+)/g, 'matched word:$1');

Upvotes: 2

Jon
Jon

Reputation: 437734

If you have to use a regex, you could go with this one:

/(?!lorem)\b\w+/gi

Upvotes: 0

FailedDev
FailedDev

Reputation: 26940

This gets ALL words in the text except Lorem.

result = subject.match(/\b(?!Lorem\b)\w+\b/g);

Not sure if this is what you were looking for though!

Explanation:

// \b(?!\bLorem\b)\w+\b
// 
//    Assert position at a word boundary «\b»
//    Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!\bLorem\b)»
//    Match the characters “Lorem” literally «Lorem»
//    Assert position at a word boundary «\b»
//    Match a single character that is a “word character” (letters, digits, etc.) «\w+»
//    Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
//    Assert position at a word boundary «\b»

Upvotes: 3

Related Questions