I was studying intro to regex stuff today and I came across this post on SO
to test these patterns: /video/123/slug and /video/123, this regular expression was given:
([^/]+)/([0-9]+)(?:/([^/]+))?
I understand basically how it works, however, it only works when I put it in a constructor regex:
var test1 = new RegExp("([^/]+)/([0-9]+)(?:/([^/]+))?"),
a = "/video/123/slug",
b = "/video/123";
console.log(test1.test(a)); //true
but doing it literally fails:
/([^/]+)/([0-9]+)(?:/([^/]+))?")/.test(a) // all sorts of warnings and an error: Uncaught SyntaxError: Unexpected token )
Why is this? http://jsbin.com/idegal/1/edit
/is needed in the literal version! (Since you start and close the expression with that same character!)/'s are not escaped