Can't remove empty line break in textarea with

2019-08-23 03:50发布

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!

3条回答
手持菜刀,她持情操
2楼-- · 2019-08-23 04:10

A regex would work like so, if you were to go that way:

str.replace(/^$/m, '');  

Yes? Someone sanity check this for me?

查看更多
可以哭但决不认输i
3楼-- · 2019-08-23 04:11
value=value.replace(/^(\r\n)|(\n)/,'');

Seems to do the trick.

DEMO

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

查看更多
We Are One
4楼-- · 2019-08-23 04:25

Or just add \t after the first replace:

value=value.replace(/(Customer account\t|Name\t|Street name\t|Telephone\t|City\t|postcode\t|Extension\t)/g,'');

The problem arises, because you have a tab after the last title.

查看更多
登录 后发表回答