我在textarea中有一个文本,我使用.value属性读取它。
现在我想从我的文本中删除所有的换行符(当你按Enter时产生的字符)现在使用正则表达式替换,但我如何在正则表达式中指示换行符?
如果不可能,还有别的办法吗?
我在textarea中有一个文本,我使用.value属性读取它。
现在我想从我的文本中删除所有的换行符(当你按Enter时产生的字符)现在使用正则表达式替换,但我如何在正则表达式中指示换行符?
如果不可能,还有别的办法吗?
当前回答
在mac上,只需在regexp中使用\n来匹配换行符。代码是字符串。Replace (/\n/g, "), ps:后面的g表示匹配所有,而不仅仅是第一个。
在窗户上,它将是\r\n。
其他回答
简单,我们可以删除新的行使用文本。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);
试试下面的代码。它适用于所有平台。
var break_for_winDOS = 'test\r\nwith\r\nline\r\nbreaks';
var break_for_linux = 'test\nwith\nline\nbreaks';
var break_for_older_mac = 'test\rwith\rline\rbreaks';
break_for_winDOS.replace(/(\r?\n|\r)/gm, ' ');
//output
'test with line breaks'
break_for_linux.replace(/(\r?\n|\r)/gm, ' ');
//output
'test with line breaks'
break_for_older_mac.replace(/(\r?\n|\r)/gm, ' ');
// Output
'test with line breaks'
要删除新的行字符,使用以下命令:
yourString.replace(/\r?\n?/g, '')
然后你可以删除字符串的前导和尾随空格:
yourString.trim()
Const text = 'test\nwith\nline\nbreaks'
const textwithoutbreak = text.split('\n')。加入(' ')
如果碰巧你不需要这个htm字符  shile使用str.replace(/(\r\n|\n|\r)/gm, ""),你可以使用这个str.split('\n').join(");
干杯