在字符串中的特定位置添加换行符

时间:2018-10-16 09:16:08

标签: javascript jquery

我不确定如何在js字符串中添加换行符。下面是代码:

{ 
  "id": 7, 
  "last": 1, 
  "name": "Customer received Email to change the password/Received. And code that customer  hasn’t requested" 
},

我想在password/Received之后换行。我想要所需的输出为

Customer received Email to change the password/Received.
And code that customer  hasn’t requested

2 个答案:

答案 0 :(得分:1)

如果您要添加换行符并且无法修改文本的原点,则可以执行以下操作:

str = "Customer received Email to change the password/Received. And code that customer  hasn’t requested";
str.replace("password/Received. ","password/Received.\n");

如果要以HTML输出,请用\n替换<br/>

答案 1 :(得分:0)

根据要在哪里输出字符串,需要其他换行符。

如果要将其输出到网页或电子邮件中,则可以使用<br />标签。

"Customer received Email to change the password/Received.<br />And code that customer  hasn’t requested"

如果要在警报或控制台中输出此消息,可以使用\n

"Customer received Email to change the password/Received.\nAnd code that customer  hasn’t requested"