JavaScript Platform Independent Line separator

前端 未结 6 1172
一整个雨季
一整个雨季 2020-12-09 09:03

Does something similar to Java\'s System.getProperty(\"line.separator\"); exist in JavaScript?

Edit: I am using the non-browser JavaScript environment

6条回答
  •  我在风中等你
    2020-12-09 09:49

    This did it for me (though I stumbled onto this question first).

    JavaScript Platform Independent Line separator

    function getLineBreakSequence() {
        var div, ta, text;
    
        div = document.createElement("div");
        div.innerHTML = "";
        ta = div.firstChild;
        text = ta.value;
        return text.indexOf("\r") >= 0 ? "\r\n" : "\n";
    }
    

    The question there is focused on Node.js use, but that answer describes the cross browser and cross platform situation well.

提交回复
热议问题