0

I am trying to replace the \n or \r to an empty spaces which comes with some other additional word. But when I tried it never replace the value because it understand it as a new line. For example this is the sample string str = "i need help \n"

I even tried using string.replace(/\r?\n|\r/g, " ") but it didn't work for me.

service.add = function(req) {
    var str= req.inputFromUI;

    if(str.indexOf("\n") > -1) {
        req.inputFromUI= str.replace("\n", " ");
    }
}

Note:

I will always have \n or \r from the test box and the requirement is something I can't restrict \n or \r in the UI. If I tried with the normal letter or word it worked for me. But the issue is only with the \n. I use my whole day to fix it but couldn't able to fix it. Can anyone help me to fix it PLEASE!! Thank you in advance. FYI: I am using angularjs.

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Kabiraj Kharel
  • 203
  • 2
  • 11
  • [check-whether-string-contains-a-line-break](https://stackoverflow.com/questions/15131072/check-whether-string-contains-a-line-break) – HDJEMAI Jun 03 '19 at 23:13

1 Answers1

1

"\\n"

In Javascript, "\n" is one character: the newline. The backslash serves to indicate that an escape sequence is coming and the n completes it. If you want to refer to the two-character string \n (note the missing quotes), then you need to escape the escape: \\.

Max Vu
  • 464
  • 2
  • 10