Remove stopwords with javascript and regex -
i want remove stopwords text fail use regex , variables properly. example remove stopword "he" affects word "when". tried use word boundaries this:
new regexp('\b'+stopwords[i]+'\b' , 'g')
doesn't work...
see small example here: jsfiddle
var stopwords = ['as', 'at', 'he', 'the', 'was']; (i = 0; < stopwords.length; i++) { str = str.replace(new regexp(stopwords[i], 'g'), ''); }
something maybe
str = str.replace(new regexp('\\b('+stopwords.join('|')+')\\b', 'g'), '');
you have double escape in regexp, , join creating
/\b(as|at|he|the|was)\b/g
Comments
Post a Comment