lundi 13 novembre 2017

How to allow only certain words consecutively with Regex in javascript

Here is what I am trying to do: I want allow only words as below, I need a regex where it should give me false if the String is not in below format, if it is then true.

"UA-1234-1,UA-12345-2,UA-34578-2" ==> should result in true

"UA-1234-1,U-12345-2" ==> should result in false. or null. Note that A is missing after U.

It should always give me true when the string passed to regex is "UA-1234-1,UA-12345-2,UA-34578-2,..........."

Below is what I am trying to do but it is matching only the first element and not returning null.

var pattern=/^UA-[0-9]+(-[0-9]+)?/g;

pattern.match("UA-1234-1,UA-12345-2,UA-34578-2");

pattern.exec("UA-1234-1,UA-12345-2,UA-34578-2)

Thanks in advance. Help is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire