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

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

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


当前回答

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

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(输出)

“书”

其他回答

这将用空格替换换行符。

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

阅读这篇文章。

var str = "bar\r\nbaz\nfoo";

str.replace(/[\r\n]/g, '');

>> "barbazfoo"

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

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

regex中的换行符是\n,因此您的脚本将是

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

简单,我们可以删除新的行使用文本。Replace (/\n/g, " ")

const text = '学生明年\n GO \n For Trip \n'; console.log("Original: ", text); Var removed_new_line = text。Replace (/\n/g, " "); console.log("New: ", removed_new_line);