2
votes

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/

4
Do you really want a regexp? I think this can be done much easier without.pimvdb
Wait; do you want a list of all words except "lorem" or just a version of the text with all instances of "lorem" removed? Does case matter?NullUserException
Indeed, as a practical matter it seems that either "split text into array of words and remove all instances of lorem" or "replace all instances of lorem with the empty string" would be better than involving regexes.Jon
@pimvdb 100% sure. @ NullUserException The word lorem has to stay in the text in this scenario.Andrew
What exactly do you mean by "select every word except 'lorem'"? What exactly do you want to accomplish with this? Is case important? (eg: should it select only 'Lorem' or 'lorem' and 'LOReM' as well)?NullUserException

4 Answers

3
votes

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»
2
votes

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');
0
votes

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

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

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.