无法使用 Javascript 替换方法删除 textarea 中的空换行符 [英] Can't remove empty line break in textarea with Javascript replace method

查看:37
本文介绍了无法使用 Javascript 替换方法删除 textarea 中的空换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将以下内容粘贴到文本区域时,当 onblur 时,它应该整理粘贴的文本,删除某些单词、制表符空格,并通过将由制表符空格分隔的每个值放入自己的行中来进行整理.但它总是在第一行留下一个空白的空行:

注意:我似乎无法模拟 html 中的制表符空间,因此您必须在记事本中手动键入以下内容才能正确复制我的问题:其中 %%% 替换为记事本中的制表符空间,然后选择-全部复制/粘贴到我的 js fiddle example textarea

 客户帐户 %%% 姓名 %%% 电话 %%% 街道名称 %%% 城市 %%% 邮政编码%%% 1234LA3 %%% KCI ASDFGHJ %%% 1234567890 %%% 10 示例 ST %%% 示例城市 %%% 1234

当你模糊它最终看起来像这样:

<前>1234LA3KCI ASDFGHJ123456789010 示例 ST示例城市第1234章

注意1234LA3"上方的空白区域.

我已经尝试在我的 .replace() 方法中添加 \n\r 但似乎仍然没有过滤掉这个空行.

任何形式的帮助将不胜感激.谢谢!

解决方案

value=value.replace(/^(\r\n)|(\n)/,'');

似乎可以解决问题.

演示

此外,您可以使用 &#09; 模拟 html 中的制表符,更不用说您不需要使用 javascript: 在事件中添加 javascript 前缀.

When I paste the following into the text area, when onblur it's supposed to tidy up the text pasted removing certain words, tab spaces and tidying up by placing each value separated by the tabs spaces into it's own line. But it always leaves a blank empty line at the very first line:

Note: I can't seem to emulate tab spaces in html so you'll have to manually type the following in notepad to properly replicate my problem: where %%% replace with tab space in notepad then select-all and copy/paste into my js fiddle example textarea

  Customer account %%% Name %%% Telephone   %%%  Street name %%% City %%% postcode%%% 1234LA3 %%% KCI ASDFGHJ %%% 1234567890 %%% 10 EXAMPLE ST %%% EXAMPLE CITY %%% 1234    

when you onblur It ends up looking like this:


1234LA3 
KCI ASDFGHJ 
1234567890 
10 EXAMPLE ST 
EXAMPLE CITY 
1234

notice blank empty space above '1234LA3'.

I've already tried adding \n\r in my .replace() method but still doesn't seem to filter out this empty line.

any kind of help will be greatly appreciated. thank you!

解决方案

value=value.replace(/^(\r\n)|(\n)/,'');

Seems to do the trick.

DEMO

Also, you can emulate tab characters in html with &#09;, not to mention that you don't need to prefix javascript in events with javascript:.

这篇关于无法使用 Javascript 替换方法删除 textarea 中的空换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆