Say I have a string like this:
1a2b3c4def
I want to split it into the following substrings, or an array:
1a
2b
3c
4debf
Every substring should start with a number character.
Another example:
Input: 1fw3fe.w6=3\657
Output:
1fw
3fe.w
6=
3\
6
5
7
If there is no number in the string, then do nothing.
What functions should I choose?
Update:
No more specific rules, just random input. I forgot to mention, the first character will ALWAYS be a digit. So if there are no more digits, put the whole string in an array.
Simple regex should do
const parts = '1fw3fe.w6=3\657'.match(/\d[^\d]*/g)
const parts = String.raw`1fw3fe.w6=3\657`.match(/\d[^\d]*/g); console.log(parts);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.