Regex again - removing small words from a string Javascript

javascript, regex

Solution

All you need is to add the global modifier to the regex. `g` and you're golden.

var myString = "abc d ef";
    myString = myString.replace(/\W*\b\w{1,2}\b/g, "");
alert(myString);

Problem

I'm trying to remove all words less than 3 characters long from a string. I've found similar questions e.g. here and here, but the accepted answers don't seem to work for me. I have a string eg. "abc d ef" and I want to achieve "abc" The (JS) code I'm currently using is: ``` var myString = "abc d ef"; myString = myString.replace(/\W*\b\w{1,2}\b/, ""); ``` ... but this returns "abc ef" and is only removing the first instance of a small word. Do I need a "+" in there somewhere to allow multiple occurrences? Are there any regex gurus that would be able to help please? I've set up a jsfiddle.

Original source

Related problems