I want to split a string with different delimiters. And also just one time... It seems I don't get it :-(. for example:
test/date-2020-02-10Xinfo
what I want is an Array like:
[test,Date,2020-02-10,info]
I tryed several things, for example:
this.informations = this.info.map(i => i.split(/(?:\/|-|X)+/));
What I get is [test,Date,2020,02,10,info]
You may use declare a regex like
/^([^\/]+)\/([^-]+)-(\d{4}-\d{2}-\d{2})X(.*)/
See the regex demo.
Details:
^
- start of string([^\/]+)
- Group 1: one or more chars other than /
\/
- a /
char([^-]+)
- Group 2: one or more chars other than -
-
- a -
char(\d{4}-\d{2}-\d{2})
- Group 3: four digits, -
, two digits, -
, two digitsX
- X
char(.*)
- Group 4: the rest of the string.Use it like
this.info.map(i => i.match(regex).slice(1));
The .slice(1)
will remove the first item from the result array String#match(RegExp)
returns, i.e. the whole match value (since you only need the captured substrings).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments