I am trying to write a function that removes specific words from a string.
The below code works ok until the last word of the sentence, as it is not followed by a space that my regex looks for.
How can I capture the last word that is not followed by a space?
function stopwords(input) {
var stop_words = new Array('a', 'about', 'above', 'across');
console.log('IN: ' + input);
stop_words.forEach(function(item) {
var reg = new RegExp(item +'\\s','gi')
input = input.replace(reg, "");
});
console.log('OUT: ' + input);
}
stopwords( "this is a test string mentioning the word across and a about");