what is the regex in javascript for “this must be longer than 8 characters but less than 15”

Using RegEx:

if (username.match(/^.{9,14}$/g)) {
    console.log("Username ok!");
} else {
    console.log("Error: Username is not of the right length");
}
  • ^ means to start looking at the beginning of the string
  • . means all characters are acceptable
  • {9,14} means we want between 9 and 14 characters of what’s before it
  • $ means to finish looking at the end of the string

The code is also very simple without regex:

if (username.length > 8 && username.length < 15) {
    console.log("Username ok!");
} else {
    console.log("Error: Username is not of the right length");
}

Read more here: Source link