How do I split a string based on a numeric value follow by a dot '.'?

Dave Callister

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

Barmar

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I turn a string in dot notation into a nested object with a value?

How can i split string with dot in makefile

PostgreSQL: How do I split range based on the critical value?

How do I split the value of a column based on a lookup table?

How do I split a JSON object based on the existence of a text value?

How do I grep the lines based on '.' (dot)?

Split string and get numeric value

How do I get a table value based on the value of another string?

How do I determine if a string is numeric?

how to split a string based on value in javascript

How can I split a string into character labels and numeric values in R?

How to split non numeric and numeric in a string in python

How do I change the color of an infobox in shinydashboard based on a string, not a value?

How Do I Split a String Into a string[]?

Split Array Based off Increment Numeric Value

split string based on numeric substrings inside it

How can I make the histogram timeline follow the factual time instead of the numeric value of the date?

In terraform how to convert numeric value to a string of word based on variable

How to split string into an alphabetic string and a numeric string?

How do I split a string and assign an int value to each of the individual characters of the string?

c# split string value by numeric

Split string by position and numeric value in dplyr

How do I apply the string split method on a pandas dataframe based on a condition?

How do I split a string based on multiple delimiters including text within parentheses in Python3?

How do I split this string so that I only have the key : value in Java?

R: How do I sort a dataframe based on a numeric vector?

How do I split this string in javascript?

How do I split this String (Java)?

How do I split a string on a delimiter ` in Bash?