【问题标题】:javascript finding a string in a stringjavascript在字符串中查找字符串
【发布时间】:2016-01-26 15:05:06
【问题描述】:

我需要一个循环来检查字符串是否包含“\n”。

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

我需要的是一个 javascript (node.js) 脚本,它检查字符串内容是否包含“\n”以及是否包含

它需要在 "\n" 字符上拆分,然后像这样打印它

<content>Hello </content><br />
<content>bob </content><br />
<content>ben je op deze </content><br />
<content>world </content><br />
<content>bobert</content><br />

【问题讨论】:

标签: javascript node.js


【解决方案1】:
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)

【讨论】:

    【解决方案2】:

    我不知道 Node.js,但在 javascript 中可以使用 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 />");
    });
    

    【讨论】:

    • 最后一行应该是""+entry+"
      "
    猜你喜欢
    • 2011-01-11
    • 1970-01-01
    • 2014-02-19
    • 2011-07-13
    • 2014-03-26
    • 2014-02-04
    相关资源
    最近更新 更多