Why is there result divergence depending on how the regular expression is executed in Javascript?

Asked

Viewed 68 times

7

What is the reason for the result divergence found when executing the same regular expression in different ways in Javascript?

Regular expression:

^([a-z][a-z0-9]{0,30}\.)?((?!\d+\.)[a-z0-9](?:[a-z0-9-]{0,24}[a-z0-9])?)(\.[a-z]{2,4}(?:\.[a-z]{2})?)$

Method 1 (works properly):

function valida_dominio(value){
    return /^([a-z][a-z0-9]{0,30}\.)?((?!\d+\.)[a-z0-9](?:[a-z0-9-]{0,24}[a-z0-9])?)(\.[a-z]{2,4}(?:\.[a-z]{2})?)$/.test(value);
}

Method 2 (not functioning properly):

function valida_dominio(value){
      let str_pattern = '^([a-z][a-z0-9]{0,30}\.)?((?!\d+\.)[a-z0-9](?:[a-z0-9-]{0,24}[a-z0-9])?)(\.[a-z]{2,4}(?:\.[a-z]{2})?)$';
      let pattern = new RegExp(str_pattern, "i");
      return pattern.test(value);
}

The goal of method 2 is to pass regular expression as a string. But it does not work properly. For example:

  • approves the domain "cëa.br" which is invalid;
  • approves the domain "26characters-asdfasdfa.com.br" which is valid.

How to make method 2 work properly? That is, how to pass the regular expression via string without having divergence in the result?

OBS: No Regexp and in method 1 the regular expression works correctly.

  • 1

    Another difference, beyond the question of \, is the flag i method 2. By default, [a-z] only consider lower case letters, but with the flag i regex will also pick up uppercase letters

1 answer

8


When you use the backslash \ within a string, you are escaping the next character.

I mean, when you write "\." in literal string, your interpreter will understand as if you were trying to escape ., when in fact you wanted to express the literal value \..

So to write an equivalent regular expression like string, you would need to escape the backslash itself, i.e.:

"^([a-z][a-z0-9]{0,30}\\.)?((?!\\d+\\.)[a-z0-9](?:[a-z0-9-]{0,24}[a-z0-9])?)(\\.[a-z]{2,4}(?:\\.[a-z]{2})?)$"

  • 2

    That’s what I’m talking about. Another thing that may not even affect current behavior but it is important to highlight if it has gone unnoticed, is the absence of the flag i method 1 and its presence in 2. Only to avoid future problems.

  • Thank you very much! It worked perfectly! It’s something basic, but I had totally forgotten to think about the backslash! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.