Split string in JavaScript and detect line break

前端 未结 7 1090
情书的邮戳
情书的邮戳 2020-12-24 04:55

I have a small function I found that takes a string from a textarea and then puts it into a canvas element and wraps the text when the line gets to

相关标签:
7条回答
  • 2020-12-24 05:30

    In case you need to split a string from your JSON, the string has the \n special character replaced with \\n.

    Split string by newline:

    Result.split('\n');
    

    Split string received in JSON, where special character \n was replaced with \\n during JSON.stringify(in javascript) or json.json_encode(in PHP). So, if you have your string in a AJAX response, it was processed for transportation. and if it is not decoded, it will sill have the \n replaced with \\n** and you need to use:

    Result.split('\\n');
    

    Note that the debugger tools from your browser might not show this aspect as you was expecting, but you can see that splitting by \\n resulted in 2 entries as I need in my case:

    0 讨论(0)
提交回复
热议问题