-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 />
2

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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 />");
});

1 Comment

it should be "<content>"+entry+"</content><br />" at last line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.