-6

i need a loop to check if a string contains "\n".

   
var content = "Hello \n bob \n ben je op deze \n world \n bobert",

what i need is a javascript (node.js) script that checks the String content if it contains "\n" and if it does

it needs to split the on the "\n" char and then print it like this

<content>Hello </content><br />
<content>bob </content><br />
<content>ben je op deze </content><br />
<content>world </content><br />
<content>bobert</content><br />
Krupesh Kotecha
  • 2,396
  • 3
  • 21
  • 40
hunteroooox
  • 115
  • 1
  • 11
  • Not sure of node.js, but you can use split() in javascript – Sumit Pandey Oct 27 '15 at 09:25
  • There is similar question here: http://stackoverflow.com/questions/1789945/how-can-i-check-if-one-string-contains-another-substring/1789952#1789952 and here: http://stackoverflow.com/questions/2878703/split-string-once-in-javascript – Majid Yaghouti Oct 27 '15 at 09:54

2 Answers2

2
var content = "Hello \n bob \n ben je op deze \n world \n bobert";
var arr = content.split("\n"); 
var str = '';
arr.forEach(function(val){
 str += '<content>'+val+'</content><br />';
})

$("div").html(str)
Shailendra Sharma
  • 6,976
  • 2
  • 28
  • 48
0

I don't know Node.js but in javascript it can be done using split()

var content = "Hello \n bob \n ben je op deze \n world \n bobert",
content_array = content.split("\n");
content_array.forEach(function(entry) {
    document.write("<content>'+entry+'</content><br />");
});
Sumit Pandey
  • 448
  • 2
  • 9