How to convert a string to an array with JavaScriptTypeScript - Stack Overflow

How can I convert a string (which is actually an ordered list) to an array using JavaScriptTypeScrip

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:

  1. 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:

  1. 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.

Share Improve this question edited Oct 23, 2020 at 9:16 adiga 35.3k9 gold badges65 silver badges87 bronze badges asked Oct 23, 2020 at 8:52 Codehan25Codehan25 3,02412 gold badges59 silver badges109 bronze badges 8
  • 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
 |  Show 3 more ments

5 Answers 5

Reset to default 2

EDIT : 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']

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745487017a4629812.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信