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?
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?
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);
var input = "Content Management System";
var abbr = input.match(/[A-Z]/g).join('');
Adapting my answer from Convert string to proper case with javascript (which also provides some test cases):
var toMatch = "hyper text markup language";
var result = toMatch.replace(/(\w)\w*\W*/g, function (_, i) {
return i.toUpperCase();
}
)
alert(result);