How I fixed a Regular Expression issue in JavaScript when it worked in PHP

I had to validate a username in both JavaScript and PHP, and I wanted to use the same Regular Expression for both. With PHP, I had no trouble:

var reg = new RegExp("^[a-zA-Z0–9._-]{3,16}$");

When I copied it into the JavaScript through, it didn’t work. The problem was that even though I defined the “set” (criteria) I had to also include the minimum/maximum number of characters it should match. It worked as soon as I added in a {3,16} at the end.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store