I have a string that looks like this
var res = "1. This is the first task
2. This is the second task. Also, these are some extra few words on the second task
3. This is the third task"
I cannot split this string based on '.'
or '\n'
it has to be
var arr = res.split("[1.2.3.4.5.6.7.8.9.10.]");
Is there a way I can split it base on the number followe by a dot?
I tried splitting the string using the dot an the new line, and both failed due to the diversity of the response. Splitting it based on the numeric value followed by the dot seems to be the best way
Use the regexp /^\d+\.\s*/
. This matches a number followed by a dot and optional spaces at the beginning of a line.
This will include an empty string at the beginning because of the empty string before the first number. You can remove that.
var res = `1. This is the first task
2. This is the second task. Also, these are some extra few words on the second task
3. This is the third task`;
var arr = res.split(/^\d+\.\s*/m);
arr.shift(); // remove empty string at beginning
console.log(arr);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments