Regex returns False when validating board

2

I'm trying to validate a car plate in the format: ABC1234 3 Letters and 4 numbers.

let regex = new RegExp("/[a-zA-Z]{3}[0-9]{4}/");
let isvalid = regex.test('abc1234');

When I post to other sites, they recognize the word, but the above code does not recognize it.

Any idea what it might be? Thank you.

    
asked by anonymous 07.01.2018 / 22:14

3 answers

11

If you use the RegExp class you will not need to use the bars; these serve only to define a direct regular expression, without the use of the class. If when you remove the bars your expression marries other values that you should not marry, your expression is wrong. Since you did not give details about what the other strings were that the result was true, but should be false, I assumed they would be for values with more than 3 letters followed by more than 4 numbers or variants thereof

let regex = new RegExp("[a-zA-Z]{3}[0-9]{4}");

const tests = [
    'abc1234',
    'ab1234', // falta uma letra
    'abc123', // falta um número
    '1234abc', // começa com número
    'abc12345', // tem um número a mais
    'abcd1234', // tem uma letra a mais
];

for (let test of tests) {
    console.log(test, regex.test(test));
}

If this is indeed the problem, just correct the expression by adding the characters ^ and $ to set the beginning and end of each value, so to match only values that start with 3 letters followed by 4 numbers and nothing more.

let regex = new RegExp("^[a-zA-Z]{3}[0-9]{4}$");

const tests = [
    'abc1234',
    'ab1234', // falta uma letra
    'abc123', // falta um número
    '1234abc', // começa com número
    'abc12345', // tem um número a mais
    'abcd1234', // tem uma letra a mais
];

for (let test of tests) {
    console.log(test, regex.test(test));
}

Using the bars instead of the class would be:

let regex = /^[a-zA-Z]{3}[0-9]{4}$/;

const tests = [
    'abc1234',
    'ab1234', // falta uma letra
    'abc123', // falta um número
    '1234abc', // começa com número
    'abc12345', // tem um número a mais
    'abcd1234', // tem uma letra a mais
];

for (let test of tests) {
    console.log(test, regex.test(test));
}

Note that in this case it is preferable to use bar notation because the expression is constant. This leaves the code more performative and more semantic. Prefer to use the class RexExp only when the expression can vary.

    
07.01.2018 / 22:41
5

Complementing, you can also delimit using the \b meta character using literal expression, also adding the ^ that defines the beginning of the string:

let regex = /^[a-zA-Z]{3}[0-9]{4}\b/;
console.log(regex.test('abc1234')); // correto
console.log(regex.test('abc12345')); // 5 números
console.log(regex.test('abcd2345')); // 4 letras
console.log(regex.test('abc145')); // 3 números
console.log(regex.test('ab2145')); // 2 letras
    
07.01.2018 / 22:49
0

To validate the mercosul standard and current board (Br) pattern, use this simple script for validation of standard mercosul boards and current boards using Javascript (vanilla) RegEx:

let plate = "ABC1234";
let plateMerc = "ABC1D23"

const regexPlate = /^[a-zA-Z]{3}[0-9]{4}$/;
const regexPlateMerc = /^[a-zA-Z]{3}[0-9]{1}[a-zA-Z]{1}[0-9]{2}$/;

function validatePlate(plate) {
  if(regexPlate.test(plate)){
    console.warn('Placa válida (padrão atual)');
    return true
  }
  else if(regexPlateMerc.test(plate)){
    console.warn('Placa válida (padrão mercosul)');
    return true
  }
  else {
    console.error('Placa inválida no padrão atual e mercosul');
    return false
  }  
    
06.11.2018 / 19:21