Why and I getting: "Invalid regular expression. Uncaught SyntaxError. Invalid escape."? <p>Im trying to create an html input tag that accepts only numbers entered in 1 of 2 formats, and reject all other input. </p> <p>I want to accept numbers in these formats only, including requiring the dashes:</p> <pre><code>1234-12 </code></pre> <p>and </p> <pre><code>1234-12-12 </code></pre> <p><strong>note:</strong> this is not for dates, but rather legal chapter numbers</p> <p>Everything I am reading about regex says that the following should work, but it isn't.</p> <pre><code>&lt;input class="form-control" type="text" pattern="^(\d{4}\-\d{2}\-\d{2})|(\d{4}\-\d{2})$" required /&gt; </code></pre> <p>Devtools Console Error in Chrome:</p> <blockquote> <p>Pattern attribute value <code>^(\d{4}\-\d{2}\-\d{2})|(\d{4}\-\d{2})$</code> is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^(\d{4}-\d{2}-\d{2})|(\d{4}-\d{2})$/: Invalid escape</p> </blockquote>