0

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:

  1. Hei, how are u?.
  2. I am good.
  3. And you?.

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.

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?

  • won't array of strings do instead of array of arrays? `var data = [ 'Hei, how are u?', 'I am good.', 'And you?' ]` – gp. Feb 20 '15 at 01:37
  • but how show like a list in the html using arrays of strings? –  Feb 20 '15 at 01:38
  • You should use the [split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) function to separate the original string. A possible separator might be `"."`. – ThreadedLemon Feb 20 '15 at 01:40

2 Answers2

3

sentence splitter can be used using

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

and then use ejs for array parsing.

Thanks.

Piyas De
  • 1,786
  • 15
  • 27
0

say this is the data passed to ejs template.

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

in ejs template:

<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

Community
  • 1
  • 1
gp.
  • 8,074
  • 3
  • 38
  • 39
  • i prefer not use modules if i can do without them what i want, because of this i am going to accept your answer. –  Feb 20 '15 at 03:08