Home > Net >  How can I remove any words with three or lesser characters in a string
How can I remove any words with three or lesser characters in a string

Time:09-07

Please, I need help with removing any words with three or lesser characters in a string.

Here is my code

let text = "The best things in life are free";
let result = text.replace(/^[a-zA-Z]{3}{2}{1}$/, '');

My expected result should be:

"best things life free"

CodePudding user response:

I don't think this need regex, just split with a space and filter with length bigger than 3 and join again with a space

let text = "The best things in life are free";
let result = text.split(' ').filter(i => i.length > 3).join(' ');

console.log(result)

  • Related