简体   繁体   中英

how to replace string newline in javascript or react js .\r\n

I have a message from fire back like this "Member Registration Number at row [2,3,4,5] already used. \ r \ nThe phone at row [2,3,4,5] is already used. \ r \ nThe email at row [2,3,4,5] already used. " how to replace the new line with the javascript function? I've tried a number of ways but it hasn't worked

message.replace('~(?:\[|(?!\A)\G)[^]\r\n]*\K\R+~', '<br>')

the original message is as follows

{
     "message": "Member Registration Number at row [2,3,4,5] already used.\\r\\nThe phone at row [2,3,4,5] is already used.\\r\\nThe email at row [2,3,4,5] already used. ",
     "Success": false
}

and the results are like this

Nomor Registrasi Anggota at row [2,3,4,5] already used.\r\nThe phone at row [2,3,4,5] already used.\r\nThe email at row [2,3,4,5] already used.

You can use replace function to do what you want, you just need to adapt the regex. Check the snippet I put for you.

 var response = { "message": "Member Registration Number at row [2,3,4,5] already used. \\ r \\ nThe phone at row [2,3,4,5] is already used. \\ r \\ nThe email at row [2,3,4,5] already used. Member Registration Number at row [2,3,4,5] already used.\\r\\nThe phone at row [2,3,4,5] is already used.\\r\\nThe email at row [2,3,4,5] already used. ", "Success": false }; var message = response.message.replace(/ \\ r \\ n/g,'<br>').replace(/\\r\\n/g,'<br>'); console.log(message);

EDIT: You can also chain replace , I edit the answer base on you edit

 var response = { "message": "Member Registration Number at row [2,3,4,5] already used.\\r\\nThe phone at row [2,3,4,5] is already used.\\r\\nThe email at row [2,3,4,5] already used. ", "Success": false }; var message = response.message.replace(/\\r|\\n/g,'<br>').replace(/<br><br>/g,"<br>"); console.log(message);
You can you the .replace with regex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM