如何从字符串中删除所有换行符

本文翻译自:How to remove all line breaks from a string

I have a text in a textarea and I read it out using the .value attribute. 我在textarea中有一个文本,并使用.value属性将其读出。

Now I would like to remove all linebreaks (the character that is produced when you press Enter ) from my text now using .replace with a regular expression, but how do I indicate a linebreak in a regex? 现在,我想使用正则表达式.replace从我的文本中删除所有换行符(按Enter时产生的字符),但是如何在正则表达式中指示换行符?

If that is not possible, is there another way? 如果那不可能,还有另一种方法吗?


#1楼

参考:https://stackoom.com/question/jKuD/如何从字符串中删除所有换行符


#2楼

You can use \\n in a regex for newlines, and \\r for carriage returns. 您可以在正则表达式中使用\\n换行,并在\\r输入回车符。

var str2 = str.replace(/\n|\r/g, "");

Different operating systems use different line endings, with varying mixtures of \\n and \\r . 不同的操作系统使用不同的行尾,并且\\n\\r This regex will replace them all. 此正则表达式将全部替换。


#3楼

A linebreak in regex is \\n, so your script would be 正则表达式中的换行符为\\ n,因此您的脚本应为

var test = 'this\nis\na\ntest\nwith\newlines';
console.log(test.replace(/\n/g, ' '));

#4楼

How you'd find a line break varies between operating system encodings. 换行符的查找方式因操作系统编码而异。 Windows would be \\r\\n , but Linux just uses \\n and Apple uses \\r . Windows将是\\r\\n ,但是Linux仅使用\\n而Apple使用\\r

I found this in JavaScript line breaks : 我在JavaScript换行符中发现了这一点:

someText = someText.replace(/(\r\n|\n|\r)/gm, "");

That should remove all kinds of line breaks. 那应该删除所有换行符。


#5楼

This is probably a FAQ. 这可能是一个常见问题解答。 Anyhow, line breaks (better: newlines) can be one of Carriage Return (CR, \\r , on older Macs), Line Feed (LF, \\n , on Unices incl. Linux) or CR followed by LF ( \\r\\n , on WinDOS). 无论如何,换行符(更好的是:换行符)可以是回车符(在旧版Mac上为CR, \\r ),换行符(在Unices包括Linux中为LF, \\n )或CR后跟LF( \\r\\n ,在WinDOS上)。 (Contrary to another answer, this has nothing to do with character encoding.) (与另一个答案相反,这与字符编码无关 。)

Therefore, the most efficient RegExp literal to match all variants is 因此,匹配所有变体的最有效的RegExp文字是

/\r?\n|\r/

If you want to match all newlines in a string, use a global match, 如果要匹配字符串中的所有换行符,请使用全局匹配,

/\r?\n|\r/g

respectively. 分别。 Then proceed with the replace method as suggested in several other answers. 然后按照其他几个答案中的建议进行replace方法。 (Probably you do not want to remove the newlines, but replace them with other whitespace, for example the space character, so that words remain intact.) (也许你不想删除换行,但与其他空格替换它们,例如空格字符,这样的话保持不变。)


#6楼

String.trim() removes whitespace from the beginning and end of strings... including newlines. String.trim()从字符串的开头和结尾删除空格,包括换行符。

const myString = "   \n \n\n Hey! \n I'm a string!!!         \n\n";
const trimmedString = myString.trim();

console.log(trimmedString);
// outputs: "Hey! \n I'm a string!!!"

Here's an example fiddle: http://jsfiddle.net/BLs8u/ 这是一个小提琴示例: http : //jsfiddle.net/BLs8u/

NOTE! 注意! it only trims the beginning and end of the string, not line breaks or whitespace in the middle of the string. 它仅修剪字符串的开头和结尾,而不修剪字符串中间的换行符或空格。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值