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'), ''); 

fiddle

you have double escape in regexp, , join creating

/\b(as|at|he|the|was)\b/g 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -