我在textarea中有一个文本,我使用.value属性读取它。

现在我想从我的文本中删除所有的换行符(当你按Enter时产生的字符)现在使用正则表达式替换,但我如何在正则表达式中指示换行符?

如果不可能,还有别的办法吗?


当前回答

Const text = 'test\nwith\nline\nbreaks'

const textwithoutbreak = text.split('\n')。加入(' ')

其他回答

我正在添加我的答案,这只是上面的一个插件, 对于我来说,我尝试了所有的/n选项,但它不起作用,我看到我的文本来自服务器,用双斜杠,所以我用了这个:

var fixedText = yourString.replace(/(\r\n|\n|\r|\\n)/gm, '');

在mac上,只需在regexp中使用\n来匹配换行符。代码是字符串。Replace (/\n/g, "), ps:后面的g表示匹配所有,而不仅仅是第一个。

在窗户上,它将是\r\n。

Const text = 'test\nwith\nline\nbreaks'

const textwithoutbreak = text.split('\n')。加入(' ')

要删除新的行字符,使用以下命令:

yourString.replace(/\r?\n?/g, '')

然后你可以删除字符串的前导和尾随空格:

yourString.trim()

这将删除你所有的换行,空格,不必要的字符

n n n n n n n n n n n n n n n n n n n n n n n n n console.log (str) var output = str.replace(/\n|\r|\W/g, ""); console.log(输出)

“书”