 <p>You could also do this :</p> <pre><code>var sale = parseInt(url.split('/sales/')[1], 10); if (!isNaN(sale)) { // do something } </code></pre> <p><code>parseInt()</code> returns <code>NaN</code> (Not A Number) in case of failure.</p> <hr> <p>Here is a function :</p> <pre><code>function toSaleId(url) { var id = parseInt(url.split('/sales/')[1], 10); if (!isNaN(id)) return id; } </code></pre> <p>Usage examples :</p> <pre><code>var sale = toSaleId('/sale/1234'); // 1234 var sale = toSaleId('/sale/1234/anotherworld'); // 1234 var sale = toSaleId('/sale/anotherworld/1234'); // undefined </code></pre>