简体   繁体   中英

How to convert a string to an array with JavaScript/TypeScript

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.

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." ]

尝试这个

 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 aa 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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM