0

How to split strings of format like "12-12-1990" or "12/12/1990" where - and / are the delimiters. I have seen answers using regex but I am not proficient in it. Answers regarding this will be helpful as I am a beginner

Nirmal Kumar
  • 89
  • 1
  • 7
  • 2
    Does this answer your question? [How do I split a string with multiple separators in javascript?](https://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript) – Ravi Makwana Jan 31 '20 at 06:03
  • I am unable to understand answers there – Nirmal Kumar Jan 31 '20 at 10:03

3 Answers3

2

You can pass a regex into Javascript's split operator. For example:

"12-12-1990".split(/-|\//) 
["12", "12", "1990"]

"12/12/1990".split(/-|\//) 
["12", "12", "1990"]
Ravi Makwana
  • 2,782
  • 1
  • 29
  • 41
2

try this

"2020-01-31".split(/[/-]/ig)

var dateParts1 = "2020-01-31".split(/[/-]/ig);
console.log(dateParts1);


var dateParts2 = "2020/02/21".split(/[/-]/ig);
console.log(dateParts2);

Regex explain enter image description here

Abdelrahman Gobarah
  • 1,544
  • 2
  • 12
  • 29
-1

You can use split with the delimiter on which you want to split the string

var x="12-12-1990";
console.log(x.split('-'))
ellipsis
  • 12,049
  • 2
  • 17
  • 33