Generate abbreviation from string in JavaScript using regular expressions?

I want to create an abbreviation string like "CMS" from the string "Content Management Systems", preferably using a regular expression.

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

+2


source to share


3 answers


Capturing all caps following the word boundary (just in case, entering all caps):



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

alert(abbrev);

      

+12


source


var input = "Content Management System";
var abbr = input.match(/[A-Z]/g).join('');

      



+5


source


Adapting my answer from Converting a string to a correct file with javascript (which provides some test cases as well):

var toMatch = "hyper text markup language";
var result = toMatch.replace(/(\w)\w*\W*/g, function (_, i) {
    return i.toUpperCase();
  }
)
alert(result);

      

+2


source







All Articles