Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!