Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegExp: I want to remove unnecessary words in the Sentence. How can I do it?

I have a sentence, and I want to remove some words from it.

So if I have:

"jQuery is a Unique language"

and an array that is named garbageStrings:

var garbageStrings = ['of', 'the', "in", "on", "at", "to", "a", "is"];

I want to remove the "is" and "a" in the sentence.

But if I use this: /This statement is inside a for loop. I'm looping the whole sentence and finding a match in the garbageStrings/

var regexp = new RegExp(garbageStrings[i]);

the string will become "jQuery Unique lnguge"

Notice that the "a" in language is removed from the sentence.

I didn't intend that to happen.

like image 585
Keira Nighly Avatar asked May 14 '09 05:05

Keira Nighly


2 Answers

Something like this:

function keyword(s) {
    var words = ['of', 'the', 'in', 'on', 'at', 'to', 'a', 'is'];
    var re = new RegExp('\\b(' + words.join('|') + ')\\b', 'g');
    return (s || '').replace(re, '').replace(/[ ]{2,}/, ' ');
}
like image 153
wombleton Avatar answered Sep 20 '22 23:09

wombleton


I could have sworn JavaScript had \b (word boundary), but it looks like it doesn't, try this instead:

var regex  = new RegExp("( |^)" + "a" + "( |$)", "g");
var string = "I saw a big cat, it had a tail.";

string = string.replace(regex, "$1$2");
like image 27
Chas. Owens Avatar answered Sep 18 '22 23:09

Chas. Owens