Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate an abbreviation from a string in JavaScript using regular expressions?

I want to generate an abbreviation string like 'CMS' from the string 'Content Management Systems', preferably with a regex.

Is this possible using JavaScript regex or should I have to go the split-iterate-collect?

like image 986
sharat87 Avatar asked Oct 02 '09 07:10

sharat87


1 Answers

Capture all capital letters following a word boundary (just in case the input is in all caps):

var abbrev = 'INTERNATIONAL Monetary Fund'.match(/\b([A-Z])/g).join('');

alert(abbrev);
like image 62
Sinan Ünür Avatar answered Nov 15 '22 04:11

Sinan Ünür