?
Solved

Removing Excessive Newlines From TextArea

Posted on 2003-03-21
9
Medium Priority
?
469 Views
Last Modified: 2008-02-01
Hi Everyone-

I currently use this regex to put a dash (-) after each newline character.

 textObj.value = textObj.value.replace( /(\n)([^-])/g, "$1-$2");    

I'm need to start removing excessive newlines... to prevent data input like the following like this:

 -sdfgsdfgsdfg
 -
 -
 -
 -
 -fgsdgdf
 -sdfgsdfg

I'd like to change the above into something like the following:

 -sdfgsdfgsdfg
 -fgsdgdf
 -sdfgsdfg

Can someone supply a regex to accomplish this? I'm terrible with RegEx's :(

Thanks in advance!
0
Comment
Question by:gerrygerry0001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 1

Expert Comment

by:Antithesis
ID: 8187555
I try to avoid regular expressions, as they're very complicated and generally unreliable.  However, a while back I made a String.prototype function to replace all instances of something with something else, you might find it useful:
----------
<script language="javascript">
String.prototype.replaceAll=function(ra_x,ra_y){ra_z=0;ra_t=this;while(ra_t.indexOf(ra_x)!=-1){ra_t=ra_t.substring(0,ra_t.indexOf(ra_x))+ra_y+ra_t.substring(ra_t.indexOf(ra_x)+ra_x.length,ra_t.length);ra_z++;}return ra_t;}
</script>
----------
There is a bug in JS though, all returns show up as \r\n, so to delete them onBlur, try this:
----------
<script language="javascript">
function replaceit(ri_elm)  {
     ri_elm.value = ri_elm.value.replaceall('\r\n\r\n', '');
}
</script>
<textarea onblur="replaceit(this)"></textarea>
----------
0
 
LVL 1

Expert Comment

by:Antithesis
ID: 8187559
woops, typo:
----------
<script language="javascript">
function replaceit(ri_elm)  {
    ri_elm.value = ri_elm.value.replaceAll('\r\n\r\n', '');
}
</script>
<textarea onblur="replaceit(this)"></textarea>
----------
0
 

Author Comment

by:gerrygerry0001
ID: 8189056
Hi Antithesis-

Sorry for the delay in responding... your proposed solution half works... it has a few flaws though.

I'd really rather stick to using a regex for this.. I think it owuld be more reliable.

In fact... I think I'd like to expand my request to include:

The removal of excessive dashes:

-safdgsdfg
-
-
-
-asfsf
-asdfsdfas

And the removal of excessive newlines:

-asdfasdfasfd
-asdfasfasdf
-


-asdfasdf

Can you help me with this Antithesis?? If not, can someone else?

Thanks for your time!
-asfasdfsa
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gerrygerry0001
ID: 8189058
Whoops.. forgot to mention an increase in points ;)
0
 
LVL 1

Expert Comment

by:Antithesis
ID: 8190236
I don't really understand how you want the output to look.  You want to have dashes at the beginning of every line, and you also want to get rid of excessive newlines?
0
 

Author Comment

by:gerrygerry0001
ID: 8191029
I want a function that I can apply to any textarea value that will use regular expressions to:
 *add a dash in front of every line that doesn't already have one
 *remove blank lines (or lines that have nothing but a dash in them)
 *remove any double dashes

Understand?

I want to change input like this (worst case scenario):
--------------------------------
This is my text
This is another line
-
Here's a line too
-Yet another line here

--This is more more text on it's own line

This is the last line of text
--------------------------------

Into this:
--------------------------------
-This is my text
-This is another line
-Here's a line too
-Yet another line here
-This is more more text on it's own line
-This is the last line of text
--------------------------------

I hope I was clear! Thanks for your patience Antithesis!
0
 
LVL 1

Accepted Solution

by:
Antithesis earned 1000 total points
ID: 8198297
Sorry for the lateness of my reply, had to reaquaint myself with RegExps.  I made this code, it's kinda long and repetitive, but it works exactly as you wanted:
----------
<script language="javascript">
function fix(f_elm)  {
     var tst = /\n/g;
     var rep = /(.*)\r\n(.*)/g;
                    while(tst.test(f_elm.value)) f_elm.value = f_elm.value.replace(rep, "$1-$2");
     while(/--/g.test(f_elm.value)) f_elm.value = f_elm.value.replace(/(--)/g, "-");
     f_elm.value = "-"+ f_elm.value.replace(/(-)/g, "\n-");
}
</script>
----------
Call it like this:
----------
<textarea onchange="fix(this)"></textarea>
----------
0
 

Author Comment

by:gerrygerry0001
ID: 8207915
Close enough!

Thanks Antithesis!
0
 

Author Comment

by:gerrygerry0001
ID: 8267172
Just in case someone else ever comes across a problem like this, here's the extended solution I went with:

function doubleCheck(textObj)
{
 if(textObj.value=="" || textObj.value=="-"){textObj.value=""; return;}
 firstDash(textObj); //just makes sure 1st char is a dash
 textObj.value = textObj.value.replace(/((\r\n)+)/g, "\n");
 textObj.value = textObj.value.replace(/((--)+)/g, "-\n-");
 textObj.value = textObj.value.replace(/((-\r\n)+)/g, "");
 textObj.value = textObj.value.replace(/^([\r\n]*)/g, "");
 textObj.value = textObj.value.replace(/([\r\n-]*)$/g, "");
}
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question