Do I need to escape dash character in regex?
<p>I'm trying to understand dash character <code>-</code> needs to escape using backslash in regex?</p>

<p>Consider this:</p>

<pre><code>var url     = '/user/1234-username';
var pattern = /\/(\d+)\-/;
var match   = pattern.exec(url);
var id      = match[1]; // 1234 
</code></pre>

<p>As you see in the above regex, I'm trying to extract the number of id from the url. Also I escaped <code>-</code> character in my regex using backslash <code>\</code>. But when I remove that backslash, still all fine ....! In other word, both of these are fine:</p>

<ul>
<li><a href="https://regex101.com/r/sJ5mX4/1" rel="noreferrer"><code>/\/(\d+)\-/</code></a></li>
<li><a href="https://regex101.com/r/sJ5mX4/2" rel="noreferrer"><code>/\/(\d+)-/</code></a></li>
</ul>

<p>Now I want to know, which one is correct <em>(standard)</em>? Do I need to escape dash character in regex? </p>