You can split a text that represents an ordered list into an array in JavaScript using the split()
method and a regular expression to capture the list items. Here's an example:
index.tsx192 chars6 lines
In this example, we're using a regular expression /^\d+\.\s/
to capture each list item. This regular expression matches one or more digits (\d+
) followed by a period (\.
) and whitespace (\s
). The ^
character matches the start of each list item.
We then use the split()
method on the listText
string, passing in our regular expression as the delimiter. This splits the listText
string into an array of strings, with each string representing one list item. We then use the slice()
method to remove the first element of the array, which will be an empty string due to our regular expression matching the start of the string.
The resulting listArray
will be an array of strings, with each string representing one item in the ordered list.
gistlibby LogSnag