最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - replace newline string literal - 'n' in javascript - Stack Overflow

programmeradmin4浏览0评论

I have string literal, \n in a variable. I am trying to replace it with empty string. But it doesn't work.

var value = "\\\n"
value = value.replace(/(?:\\[rn])+/g, "")
console.log(value)

value evaluates to the string literal - \n. I expect no output from console.log. But it prints

\

Empty line followed by a backward slash (for some reason, stack overflow has trimmed the empty line in above output).

Note, this question is not related to replacing newline character -\n

I have string literal, \n in a variable. I am trying to replace it with empty string. But it doesn't work.

var value = "\\\n"
value = value.replace(/(?:\\[rn])+/g, "")
console.log(value)

value evaluates to the string literal - \n. I expect no output from console.log. But it prints

\

Empty line followed by a backward slash (for some reason, stack overflow has trimmed the empty line in above output).

Note, this question is not related to replacing newline character -\n

Share Improve this question asked Sep 14, 2015 at 18:28 user2879704user2879704 4
  • Why would you expect no output? – rnevius Commented Sep 14, 2015 at 18:30
  • that is a smaller example of my plex problem – user2879704 Commented Sep 14, 2015 at 18:31
  • 2 "\\\n" produces a backslash followed by a line break. If your string literally contains \n, it would be "\\n". At this point I'm not sure what string value you really have. – Felix Kling Commented Sep 14, 2015 at 18:32
  • awesomee..... you have an eye for errors.... the error is in the variable, not in the pattern – user2879704 Commented Sep 14, 2015 at 18:37
Add a ment  | 

4 Answers 4

Reset to default 6

I have string literal, \n in a variable

No you don't. You have \(newline), which is a quite different thing.

var value = "\\\n"

value is a string of length two. The first character is a backslash. The second character is a newline.

value = value.replace(/(?:\\[rn])+/g, "")

Your regexp attempts to replace a literal backslash followed by either the letter r or the letter n. But your input contains a newline in the second position. A newline matches neither r nor n. Hence nothing is replaced, and value retains its original value.

To see this more clearly:

> for (i = 0; i < value.length; i++) console.log(value.charCodeAt(i));
< 92
< 10  // this is a newline

You may have been confused by some non-intuitive behavior in the Chrome devtools console. console.log('\n') prints an empty line as expected. However, console.log('a\n') prints only the a, with no apparent newline. In other words, Chrome devtools (as well as FF) appears to suppress the final trailing newline in certain situations.

Don't do like \\[rn]. ie, Don't separate backslash and r, n . This would change its meaning.

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

If you also want to replace the backslash character which exists before carriage return or newline character then add \\ before the character class.

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

You should use:

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

This will replace \r or \n which is followed by a literal \ in your input.

This one works pretty well .replace(/\\r\\n?|\\n/g, '\n');

发布评论

评论列表(0)

  1. 暂无评论