Using Javascript. (note there is a similar post, but the OP requested Java, this is for Javascript)
I'm trying to remove a list of words from an entire string without looping (preferably using Regular Expressions).
This is what I have so far, and it removes some of the words but not all of them. Can someone help identify what I'm doing wrong with my RegEx function?
//Remove all instances of the words in the array
var removeUselessWords = function(txt) {
var uselessWordsArray =
[
"a", "at", "be", "can", "cant", "could", "couldnt",
"do", "does", "how", "i", "in", "is", "many", "much", "of",
"on", "or", "should", "shouldnt", "so", "such", "the",
"them", "they", "to", "us", "we", "what", "who", "why",
"with", "wont", "would", "wouldnt", "you"
];
var expStr = uselessWordsArray.join(" | ");
return txt.replace(new RegExp(expStr, 'gi'), ' ');
}
var str = "The person is going on a walk in the park. The person told us to do what we need to do in the park";
console.log(removeUselessWords(str));
//The result should be: "person going walk park. person told need park."
Show activity on this post. var str = "I have a cat, a dog, and a goat."; str = str. replace(/goat/i, "cat"); // now str = "I have a cat, a dog, and a cat." str = str. replace(/dog/i, "goat"); // now str = "I have a cat, a goat, and a cat." str = str.
Use the replace() method to replace multiple characters in a string, e.g. str. replace(/[. _-]/g, ' ') . The first parameter the method takes is a regular expression that can match multiple characters.
Three moments:
|
without side spaces(...|...)
\b
to match a separate words var removeUselessWords = function(txt) {
var uselessWordsArray =
[
"a", "at", "be", "can", "cant", "could", "couldnt",
"do", "does", "how", "i", "in", "is", "many", "much", "of",
"on", "or", "should", "shouldnt", "so", "such", "the",
"them", "they", "to", "us", "we", "what", "who", "why",
"with", "wont", "would", "wouldnt", "you"
];
var expStr = uselessWordsArray.join("|");
return txt.replace(new RegExp('\\b(' + expStr + ')\\b', 'gi'), ' ')
.replace(/\s{2,}/g, ' ');
}
var str = "The person is going on a walk in the park. The person told us to do what we need to do in the park";
console.log(removeUselessWords(str));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With