我有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';
我想用HTML换行符替换\n并想显示如下:



我正在使用JavaScript的替换功能

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

但这不起作用。

最佳答案

使用正则表达式(RegExp)文字和“global”(g)修饰符:

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

或直接使用 RegExp :
var val = recommend.replace(RegExp("\n","g"), "<br>");

09-25 21:12