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

113
Views
String match one or more numbers with a # - Javascript / regex

I am trying to match a part of a string with another string. In string one I have a larger amount of text that has the desired string within. In string two I have a part of the desired string with the numbers replaced with '#'.

How can I match any numbers in the same position of the #?

I am guessing that there would be a regex that does this but I can't figure it out.

Example

Str1 = "Adds 2 to 5 Cold Damage Adds 20 to 50 Fire Damage Adds 1 to 100 Lightning Damage"    

Str2 = "Adds # to # Fire Damage" 

res = "Adds 20 to 50 Fire Damage"
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Replace the # with \d+ to match a number there, and convert that to a regular expression.

let Str1 = "Adds 2 to 5 Cold Damage Adds 20 to 50 Fire Damage Adds 1 to 100 Lightning Damage";
let Str2 = "Adds # to # Fire Damage";
let re = new RegExp(Str2.replaceAll('#', '\\d+'));
let res = Str1.match(re);
if (res) {
  console.log(res[0]);
}

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!