Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace '\n' with <br> in JavaScript

Tags:

javascript

I have var recommend = 'I recommended Garden Solutions for this Tender Contracting on the basis of\n\n1)Top Scorer for tender\n2)Professional Experience in Building Services\n3)Approved Service Providers';

I want to replace \n with an HTML break and want to display it as below:

I recommended Garden Solutions for this Tender Contracting on the basis of

1)Top Scorer for tender

2)Professional Experience in Building Services

3)Approved Service Providers

I am using JavaScript's replace function

var val = recommend.replace("\n","<br>");

But it's not working.

like image 545
Navdeep Avatar asked Jun 21 '26 03:06

Navdeep


1 Answers

Use a regular expression (RegExp) literal and the "global" (g) modifier:

var val = recommend.replace(/\n/g, "<br />");

Or use a RegExp directly:

var val = recommend.replace(RegExp("\n","g"), "<br>");
like image 152
KooiInc Avatar answered Jun 23 '26 18:06

KooiInc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!