I'm trying to understand dash character -
needs to escape using backslash in regex?
Consider this:
var url = '/user/1234-username';
var pattern = /\/(\d+)\-/;
var match = pattern.exec(url);
var id = match[1]; // 1234
As you see in the above regex, I'm trying to extract the number of id from the url. Also I escaped -
character in my regex using backslash \
. But when I remove that backslash, still all fine ....! In other word, both of these are fine:
Now I want to know, which one is correct (standard)? Do I need to escape dash character in regex?