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

222
Visualizações
How do I pass a variable into regex with Node js?

So basically, I have a regular expression which is

var regex1 = /10661\" class=\"fauxBlockLink-linkRow u-concealed\">([\s\S]*?)<\/a>/;

var result=text.match(regex1);
user_activity = result[1].replace(/\s/g, "")
console.log(user_activity);

What I'm trying to do is this

var number = 1234;
var regex1 = /${number}\" class=\"fauxBlockLink-linkRow u-concealed\">([\s\S]*?)<\/a>/;

but it is not working, and when I tried with RegExp, I kept getting errors.

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

0

You can use RegExp to create regexp from a string and use variables in that string.

var number = 1234;
var regex1 = new RegExp(`${number}aa`);

console.log("1234aa".match(regex1));

about 4 years ago · Juan Pablo Isaza Relatório

0

You can build the regex string with templates and/or string addition and then pass it to the RegExp constructor. One key in doing that is to get the escaping correct as you need an extra level of escaping for backslashes because the interpretation of the string takes one level of backslash, but you need one to survive as it gets to the RegExp contructor. Here's a working example:

function match(number, str) {
    let r = new RegExp(`${number}" class="fauxBlockLink-linkRow u-concealed">([\\s\\S]*?)<\\/a>`);
    return str.match(r);
}


const exampleHTML = '<a href="something" class="aaa1234" class="fauxBlockLink-linkRow u-concealed">Some link text</a>';

console.log(match(1234, exampleHTML));

Note, using regex to match HTML like this becomes very order-sensitive (whereas the HTML itself isn't order-sensitive). And, your regex requires exactly one space between classes which HTML doesn't. If the class names were in a slightly different order or spacing different in the <a> tag, then it would not match. Depending upon what you're really trying to do, there may be better ways to parse and use the HTML that isn't order-sensitive.

about 4 years ago · Juan Pablo Isaza Relatório

0

I solved it with the method of Adem,

function escapeRegExp(string) {
    return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}


var number = 1234; 

var firstPart = `<a href="/forum/search/member?user_id=${number}" class="fauxBlockLink-linkRow u-concealed">`

var regexpString = escapeRegExp(firstPart) + '([\\s\\S]*?)' + escapeRegExp('</a>');

console.log(regexpString)


var sample = `<a href="/forum/search/member?user_id=1234" class="fauxBlockLink-linkRow u-concealed"> </a>`

var regex1 = new RegExp(regexpString); 

console.log(sample.match(regex1));

in the first place the issue was actually the way I was reading the file, the data I was applying the match on, was undefined.

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