javascript replace() not replacing text containing literal \r\n strings

后端 未结 3 1759
庸人自扰
庸人自扰 2020-12-29 07:36

Using this bit of code trims out hidden characters like carriage returns and linefeeds with nothing using javascript just fine:

value = value.replace(/[\\r\\         


        
3条回答
  •  既然无缘
    2020-12-29 08:07

    To just remove them, this seems to work for me:

    value = value.replace(/[\r\n]/g, "");
    

    You don't need the * after the character set because the g flag solves that for you.

    Note, this will remove all \r or \n chars whether they are in this exact sequence or not.

    Working demo of this option: http://jsfiddle.net/jfriend00/57GtJ/


    If you want to remove these characters only when in this exact sequence (e.g. only when a \r is directly followed by a \n, you could use this:

    value = value.replace(/\r\n/g, "");
    

    Working demo of this option: http://jsfiddle.net/jfriend00/Ta3sn/

提交回复
热议问题