Skip to content

How to convert string into regex with the words more than 3 character?

I am trying to create a ABRV from string. I can achieve some result , but not the final expected one. For example, if I have INTERNATIONAL Monetary Fund ltd string, I need to get IMF from it.

I have tried this one, but it only returns IMFl. I need to restrict it to count only words with more than 3 characters.

'INTERNATIONAL Monetary Fund ltd'.match(/bwB/g).join('');

This one doesn’t work as expected

'INTERNATIONAL Monetary Fund ltd'.match(/bw{4,}B/g).join('');


To only capture the first letters of words of 4 or more characters, you can use a lookahead for 3 or more word characters after matching the first character:

const abrv = 'INTERNATIONAL Monetary Fund ltd'.match(/bw(?=w{3,})/g).join('');