Hamza Talaghzi Hamza Talaghzi - 17 days ago 5
Javascript Question

Test a sentence statement with javascript and regex

Hi everybody!
I want to test a sentence like: "int a,b,c" for example is true or false
so I used the following code :


text="int a1,a2,a3"
if(/int ((([a-z_A-Z]+[0-9]*),)+)$/g.test(text)/g){
document.write("true");
}


but the function test always return false

I don't know what the best expression I must test

Answer

You will need to add ? after the comma , to match between zero and one because the last number does not have afterward.

int ((([a-z_A-Z]+[0-9]*),?)+)$