简体   繁体   English

如何使用 JavaScript/TypeScript 将字符串转换为数组

[英]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?如何使用 JavaScript / TypeScript 将字符串(实际上是有序列表)转换为数组? Unfortunately, this is what the backend returns.不幸的是,这就是后端返回的内容。

The string looks like this:字符串如下所示:

  1. Lorem Ipsum. Lorem Ipsum。 2. Lorem Ipsum. 2. Lorem Ipsum。 3. 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: ..为了在我的 Angular 模板中像这样使用它:

<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.我已经尝试过split()JSON.parse() .. 我不知道用什么字符来拆分数组。

For example console.log (this.entries.split (''));例如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.编辑:也使用积极的前瞻来保持 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.可能有更好的正则表达式(我在 regex 方面非常糟糕)但这对于这个精确的例子来说是有效的,解决方案都取决于你应用的正则表达式。 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. \\s - 匹配一个空白字符。

(?=[0-9]) - Look (on positive side - meaning forward) for a numeric character. (?=[0-9]) - 查找(正面 - 意思是向前)一个数字字符。

The .split() method applies the split on a match. .split()方法将拆分应用于匹配项。 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我知道这可能不是问题的确切答案,但以防万一有人想将字符串转换为单个字符的数组,您可以尝试如图所示的展开运算符而不是“quanky”循环

const myString = 'a very good programmer' const myString = '一个非常好的程序员'

const myStringArr = [...myString] 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'] console.log(myStringArr) ['a', ' ', 'v', 'e', 'r', 'y', ' ', 'g', 'o', 'o', 'd', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'e', 'r']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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