RegEx to match list

I’m using javascript RegEx to validate a user input that gets a string containing a list of numbers like 3 - 4 - 5, or 3 / 4 / 5, or 3 , 4 , 5.

So far my regex is

function(string){
    var reg = /([0-9])([^s]|[s])([-,/])/g;
    if(string.match(reg){
        return true;
    }
    return false;
}

So far I can get 3 - 4 - but not 5, and if I omit the whitespace it does not match anything.

I want a regex that could get all of n numbers divided by those characters ( - ,), and not match alphabetic or other special chars.

Any suggestion?


Source: regex

Leave a Reply