如何在有序列表中显示字符串的一部分

时间:2022-09-13 16:02:50

I have an String:

我有一个字符串:

var data = 'Hei, how are u?. I am good. And you?.'

I am using EJS as my view engine, and i want show like this:

我使用EJS作为我的视图引擎,我希望显示如下:

  1. Hei, how are u?.
  2. 嘿,你好吗?
  3. I am good.
  4. 我很好。
  5. And you?.
  6. 你呢?。

Currently i am using an array of arrays:

目前我正在使用数组数组:

var data = [
  ['Hei, how are u?'],
  ['I am good.'],
  ['And you?']
]

Them i can do a for loop and show like a list.

他们我可以做一个for循环并像列表一样显示。

This was the only way I could do, but I believe I'm doing wrong.

这是我能做的唯一方法,但我相信我做错了。

Can someone give me a hand here?

有人可以帮我一把吗?

How break the string and show like a list in the HTML?

如何打破字符串并像HTML中的列表一样显示?

2 个解决方案

#1


0  

say this is the data passed to ejs template.

说这是传递给ejs模板的数据。

var data = { parts: [ 'Hei, how are u?', 'I am good.', 'And you?' ] };

in ejs template:

在ejs模板中:

<ol>
<% for(var i = 0; i < parts.length; i++) {%>
    <li><%= parts[i] %></li>
<% } %>
</ol>

note: use regex to split string into parts matching '.', '?' etc.

注意:使用正则表达式将字符串拆分为匹配'。','?'的部分等等

var txt = 'Hei, how are u?. I am good. And you?.';
var parts = txt.match(/[^\.!\?]+[\.!\?]+/g);
var data = { parts: parts };

regex code reference: Javascript RegExp for splitting text into sentences and keeping the delimiter

正则表达式代码引用:Javascript RegExp用于将文本拆分为句子并保留分隔符

#2


3  

sentence splitter can be used using

句子分割器可以使用

https://github.com/parmentf/node-sentence-tokenizer

https://github.com/parmentf/node-sentence-tokenizer

and then use ejs for array parsing.

然后使用ejs进行数组解析。

Thanks.

谢谢。

#1


0  

say this is the data passed to ejs template.

说这是传递给ejs模板的数据。

var data = { parts: [ 'Hei, how are u?', 'I am good.', 'And you?' ] };

in ejs template:

在ejs模板中:

<ol>
<% for(var i = 0; i < parts.length; i++) {%>
    <li><%= parts[i] %></li>
<% } %>
</ol>

note: use regex to split string into parts matching '.', '?' etc.

注意:使用正则表达式将字符串拆分为匹配'。','?'的部分等等

var txt = 'Hei, how are u?. I am good. And you?.';
var parts = txt.match(/[^\.!\?]+[\.!\?]+/g);
var data = { parts: parts };

regex code reference: Javascript RegExp for splitting text into sentences and keeping the delimiter

正则表达式代码引用:Javascript RegExp用于将文本拆分为句子并保留分隔符

#2


3  

sentence splitter can be used using

句子分割器可以使用

https://github.com/parmentf/node-sentence-tokenizer

https://github.com/parmentf/node-sentence-tokenizer

and then use ejs for array parsing.

然后使用ejs进行数组解析。

Thanks.

谢谢。