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

281
Views
Add a variable in RegExp

I would like to use a variable in RegExp in this function.

checkSocial(platform, link) {
let reg = new RegExp(/^(?:https?:\/\/)?(www\.facebook\.com\/)(?:\S+)/);
return reg.test(link);
}

Doing this:

let reg = new RegExp(`/^(?:https?:\/\/)?(www\.${platform}\.com\/)(?:\S+)/`);

or this:

let reg = new RegExp('^(?:https?:\/\/)?(www\.' + platform + '\.com\/)(?:\S+)');

didn't work. How can I add a variable?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You don't need the slashes when creating RegExp this way, it will add them automatically.

Also, note you would need extra escaping - I think the last \S token was not escaped properly?

This seems to be a more valid pattern:

let platform = 'facebook';
let reg1 = new RegExp(`^(?:https?:\/\/)?(www\.${platform}\.com\/)(?:\S+)`)
let reg2 = new RegExp(`^(?:https?:\/\/)?(www\.${platform}\.com\/)(?:\\S+)`)
console.log("1", reg1) // Outputs: /^(?:https?:\/\/)?(www.facebook.com\/)(?:S+)/
console.log("2", reg2) // Outputs: /^(?:https?:\/\/)?(www.facebook.com\/)(?:\S+)/

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!