• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

Removing Excessive Newlines From TextArea

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
gerrygerry0001
Asked:
gerrygerry0001
  • 5
  • 4
1 Solution
 
AntithesisCommented:
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
 
AntithesisCommented:
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
 
gerrygerry0001Author Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
gerrygerry0001Author Commented:
Whoops.. forgot to mention an increase in points ;)
0
 
AntithesisCommented:
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
 
gerrygerry0001Author Commented:
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
 
AntithesisCommented:
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
 
gerrygerry0001Author Commented:
Close enough!

Thanks Antithesis!
0
 
gerrygerry0001Author Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now