Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
expression stored in variable as string

I have this code that works.

myDate = str.match(/(\d{1,4}([.\-/])\d{1,2}([.\-/])\d{1,4})/g);

However I would like to store the expression as a variable and use that instead. Something like this:

pattern = "/(\d{1,4}([.\-/])\d{1,2}([.\-/])\d{1,4})/g"; // I understand this is wrong somehow

myDate = str.match(pattern);

How do I store my expression as a variable and use it the way I have shown?

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Remove the quotations:

pattern = /(\d{1,4}([.\-/])\d{1,2}([.\-/])\d{1,4})/g;

myDate = "01-01-2021".match(pattern);

console.log(myDate);

about 4 years ago · Juan Pablo Isaza Relatório

0

As with most things in JavaScript, a Regular Expression pattern is an object - specifically, a RegExp object. As explained in MDN:

There are two ways to create a RegExp object: a literal notation and a constructor.

  • The literal notation's parameters are enclosed between slashes and do not use quotation marks.
  • The constructor function's parameters are not enclosed between slashes but do use quotation marks.

The additional point relevant to your example is that the g flag is added on the end of the literal notation, but as a separate parameter in the constructor function. So either of the following will work:

pattern = /(\d{1,4}([.\-/])\d{1,2}([.\-/])\d{1,4})/g;
pattern = new RegExp('\d{1,4}([.\-/])\d{1,2}([.\-/])\d{1,4})', 'g');

The reason your attempt didn't give an error, but didn't match the string is explained on the MDN page for the match function:

If regexp is a non-RegExp object, it is implicitly converted to a RegExp by using new RegExp(regexp).

So your code was equivalent to this:

pattern = "/(\d{1,4}([.\-/])\d{1,2}([.\-/])\d{1,4})/g";
myDate = str.match(new RegExp(pattern));

When what you wanted was this:

pattern = "(\d{1,4}([.\-/])\d{1,2}([.\-/])\d{1,4})";
myDate = str.match(new RegExp(pattern, "g"));
about 4 years ago · Juan Pablo Isaza Relatório

0

Ok, after playing about I got it to work by taking the qoutation marks away by doing this.

var pattern=/(\d{1,4}([.\-/])\d{1,2}([.\-/])\d{1,4})/g;
myDate = str.match(pattern);
                
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda