How replace HTML
with newline character “\n”

后端 未结 3 1958
栀梦
栀梦 2020-12-16 09:57

How can I replace HTML

or
with new line character \"\\n\"

3条回答
  •  無奈伤痛
    2020-12-16 10:48

    This function considers whether to remove or replace the tags such as
    ,
    ,
    ,
    .

    /**
     * This function inverses text from PHP's nl2br() with default parameters.
     *
     * @param {string} str Input text
     * @param {boolean} replaceMode Use replace instead of insert
     * @return {string} Filtered text
     */
    function br2nl (str, replaceMode) {   
    
      var replaceStr = (replaceMode) ? "\n" : '';
      // Includes 
    ,
    ,
    ,
    return str.replace(/<\s*\/?br\s*[\/]?>/gi, replaceStr); }

    In your case, you need to use replaceMode. For eaxmple: br2nl('1st
    2st', true)

    Demo - JSFiddle

    JavaScript nl2br & br2nl functions

提交回复
热议问题