How can I convert a string (which is actually an ordered list) to an array using JavaScript / TypeScript? Unfortunately, this is what the backend returns.
The string looks like this:
- Lorem Ipsum. 2. Lorem Ipsum. 3. Lorem Ipsum.
I want an array like this:
[
'1. Lorem Ipsum.',
'2. Lorem Ipsum.',
'3. Lorem Ipsum.'
]
..in order to use it like this in my Angular template:
<div>
<ol>
<li *ngFor="let entry of entries">{{entry}}</li>
</ol>
</div>
I already tried it with split()
and JSON.parse()
.. I don't know what character to use to split the array.
For example console.log (this.entries.split (''));
returns an array with each word of the string. I also tried using other characters to split the string but I can't find the right one.
How can I convert a string (which is actually an ordered list) to an array using JavaScript / TypeScript? Unfortunately, this is what the backend returns.
The string looks like this:
- Lorem Ipsum. 2. Lorem Ipsum. 3. Lorem Ipsum.
I want an array like this:
[
'1. Lorem Ipsum.',
'2. Lorem Ipsum.',
'3. Lorem Ipsum.'
]
..in order to use it like this in my Angular template:
<div>
<ol>
<li *ngFor="let entry of entries">{{entry}}</li>
</ol>
</div>
I already tried it with split()
and JSON.parse()
.. I don't know what character to use to split the array.
For example console.log (this.entries.split (''));
returns an array with each word of the string. I also tried using other characters to split the string but I can't find the right one.
- you already tried with split, and why is it not working ? – Crocsx Commented Oct 23, 2020 at 8:54
-
can you modify the string ? so you can add spesific delimeter for
split()
. – Rio A.P Commented Oct 23, 2020 at 8:56 - You can use split() and "pop" the index strings. – Vucko Commented Oct 23, 2020 at 8:57
- @RapSherlock No, currently the API returns the string like this :( – Codehan25 Commented Oct 23, 2020 at 9:01
- @Vucko Hmm, how? – Codehan25 Commented Oct 23, 2020 at 9:01
5 Answers
Reset to default 2EDIT : Use also positive lookahead to keep the 1. 2. 3.
There might be better regex ( I am very bad at regex ) but this do the trick for this precise exemple, the solution all depend on the regex you apply. but the method to use is split.
var text = "1. Lorem Ipsum. 2. Lorem Ipsum. 3. Lorem Ipsum.";
var regex = new RegExp("(?=[0-9].)");
text.split(regex);
=> OUTPUT
Array(3) [ "1. Lorem Ipsum. ", "2. Lorem Ipsum. ", "3. Lorem Ipsum." ]
Try this
console.log("1. Lorem Ipsum. 2. Lorem Ipsum. 3. Lorem Ipsum.".match(/\d+\D+/g))
if you cannot have a better end of sentence then your current .
you will have to building some smart splinting and filtering
something like this works:
text = "1. Lorem Ipsum. 2. Lorem Ipsum. 3. Lorem Ipsum."
arr = text.split(".")
res = text.split(".").map((item, i)=> (i%2 === 0) ? `${item}.${arr[i+1]}.` : "undefined" ).filter(item=> !item.includes("undefined"))
obliviously this is not optimized, but i am sure you can start from there
Something like this should work:
var a = '1. Lorem Ipsum. 2. Lorem Ipsum. 3. Lorem Ipsum.';
var split = a.split(/\s(?=[0-9]/);
console.log('output',split); // Prints: ["1. Lorem Ipsum.", "2. Lorem Ipsum.", "3. Lorem Ipsum."]
The regex basically says:
\s
- Match a whitespace character.
(?=[0-9])
- Look (on positive side - meaning forward) for a numeric character.
The .split()
method applies the split on a match. But the lookahead is necessary to ascertain that a number exists after the match.
I know this might not be the exact answer to the question but just in case someone wants to convert a a string to an array of individual characters, you can try out the spread operator as shown instead of the 'quanky' loops
const myString = 'a very good programmer'
const myStringArr = [...myString]
console.log(myStringArr) ['a', ' ', 'v', 'e', 'r', 'y', ' ', 'g', 'o', 'o', 'd', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'e', 'r']