Regex how can i allow zero or one slash

Viewed 20

I'm trying to allow zore or one slash.

let reg = new RegExp('\/?');
    console.log(reg.test("test//123"));

User can enter slash anywhere. if slash exist more than one it must return false

Solution @Adrian Shum:

 let reg = new RegExp('^[^/]*/?[^/]*$');
        console.log(reg.test("test//123/"));

0 Answers
Related