Sunday, January 1, 2012

Javascript Regular Expression to match a comma delimited list

This method uses the JavaScript String's match() method to parse a comma delimited list and show its content in a Panel control.

You can try it here http://jsfiddle.net/38tXU/
function parseList(list) {
    var reg = list.match(/\w+(,\w+)*/g);
    document.getElementById('pnl').innerHTML = 'Total length:' + reg.length + '<br />';
    for (var i = 0; i < reg.length; i++) {
        document.getElementById('pnl').innerHTML += 'Token:\'' + reg[i] + '\'<br />';
    }
}
//Call parseList function
parseList('item1, item2, item3');

Source: https://www.nilebits.com/blog/2010/12/javascript-regular-expression-to-match-a-comma-delimited-list/