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

164
Views
How can I extract numbers between slashes in javascript

I have a strings with this pattern,

ver1.1/hello/12345/bar -> extract 12345
world/098767123/foo   -> extract 098767123
ver1.2344/foo/78687115/ -> extract 78687115

I used /\d+/g or /\d+/, but didn't have luck that always ver numbers come too. How can I extract numbers which does not have any char and between slashes?

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

0

Use a capturing group to capture only the numbers between slashes: \/(\d+)\/

console.log("ver1.1/hello/12345/bar".match(/\/(\d+)\//))

about 4 years ago · Juan Pablo Isaza Report

0

You can use a capture group for this, e.g.:

var regex = /\/(\d+)\//;
var string = "ver1.2344/foo/78687115/";
var match = string.match(regex);
if (match) {
    console.log(match[1]);
}

You can also use /(?:\/|^)(\d+)(?:\/|$)/ for numbers that happen at the beginning or end of the string. This just has two non-capturing groups that say "match / or the beginning/end"

about 4 years ago · Juan Pablo Isaza Report

0

You can add a map to the output of the match to strip out the slashes. The use of the global flag g also prevented duplicate results.

const regEx = /(?:\/)(\d+)(?:\/)/g;
const source = "ver1.1/hello/12345/bar";
const arrResults = Array.from(source.matchAll(regEx), m => m[1]);
console.log(arrResults)

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!