Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

221
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda