admin管理员组

文章数量:1403339

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!

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!

Share Improve this question asked Nov 3, 2012 at 1:38 like-a-traineelike-a-trainee 5613 gold badges13 silver badges22 bronze badges 4
  • You said onblur...thats ondblclick. You really should separate your javascript into a function that you call in your element's inline event handler...much easier to manage and reuse. – Ian Commented Nov 3, 2012 at 1:41
  • You can use just "\n", like: jsfiddle/3PSzj/1 – Patrick Moore Commented Nov 3, 2012 at 1:43
  • I tried but like with Daedalus it just creates more empty lines above the text.. – like-a-trainee Commented Nov 3, 2012 at 2:00
  • thank you everyone, I bined Daedulus's line removal and Manatax's tab removal together. Works like a charm now... jsfiddle/themeanfiddler/3PSzj/6 – like-a-trainee Commented Nov 3, 2012 at 3:01
Add a ment  | 

3 Answers 3

Reset to default 3
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:.

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.

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

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

Yes? Someone sanity check this for me?

本文标签: Can39t remove empty line break in textarea with Javascript replace methodStack Overflow