最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

line breaks using javascript strings - Stack Overflow

programmeradmin5浏览0评论

I m breaking a line in javascript using \n for example: -

text = "this is a test" + "\n" + "another test";

and to show that in html i m using the code

text = text.replace('\n' , '<br>');

but the text i m getting is without the br

when checking in firebug console i get is with line break(\n not shown but newline created)

how can i place line breaks using \n or i have to do some custom code instead of \n

thanks

I m breaking a line in javascript using \n for example: -

text = "this is a test" + "\n" + "another test";

and to show that in html i m using the code

text = text.replace('\n' , '<br>');

but the text i m getting is without the br

when checking in firebug console i get is with line break(\n not shown but newline created)

how can i place line breaks using \n or i have to do some custom code instead of \n

thanks

Share Improve this question edited Dec 29, 2010 at 21:31 Pratik 11.7k22 gold badges70 silver badges99 bronze badges asked Dec 29, 2010 at 21:28 Pradyut BhattacharyaPradyut Bhattacharya 5,74813 gold badges58 silver badges86 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 5
var newText = text.replace(/\n/g , "<br>");

use double quotes to not escape the \n

try this:

text = text.replace("\n" , "<br>");

/\r\n|[\r\n]/g <- this is what I use. I am a pessimist..

发布评论

评论列表(0)

  1. 暂无评论