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

161
Views
Javascript Split and Match the pattern array

I am trying to get the pattern from a string in JavaScript, where my string is 01-01-2000 - 01-01-2010 here I want to make these both date separate like date1 = 01-01-2000 and date2 = 01-01-2010.

I have tried this code :

var date = "01-01-2000 - 01-01-2010";
var date2 = date.match(/([0-9]{2})-([0-9]{2})-([0-9]{4})/);
console.log(date2[0]);

output: 01-01-2000 [correct]

but I using console.log(date2[1]) it displaying 01 only.

Please help how to achieve the goal make this function.

expecting output :

date[0]: 01-01-2000
date[1]: 01-01-2010
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You Can use split instead If your date format is fixed.

var date = "01-01-2000 - 01-01-2010";
var date2 = date.split(" - ")
console.log(date2);

about 4 years ago · Juan Pablo Isaza Report

0

if you want to work with pattern you only have to add the /g in the end for global search into the string:

var date = "01-01-2000 - 01-01-2010";
var date2 = date.match(/([0-9]{2})-([0-9]{2})-([0-9]{4})/g);
console.log(date2[0]);
console.log(date2[1]);

about 4 years ago · Juan Pablo Isaza Report

0

You can give a try to String.matchAll() which returns an iterator of all results matching a string against a regular expression, including capturing groups.

Demo :

var date = "01-01-2000 - 01-01-2010";
var date2 = [ ...date.matchAll(/([0-9]{2})-([0-9]{2})-([0-9]{4})/g) ];

for (const match of date2) {
    console.log(match[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!