Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > RegExp for remove all trailing CrLf's?

Reply
Thread Tools

RegExp for remove all trailing CrLf's?

 
 
McKirahan
Guest
Posts: n/a
 
      01-28-2004
How would I use a regular expression to remove all trailing Carriage Returns
and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance.

Also, are they any great references for learning how to use Regular
Expressions?


 
Reply With Quote
 
 
 
 
Brian Genisio
Guest
Posts: n/a
 
      01-29-2004
McKirahan wrote:
> How would I use a regular expression to remove all trailing Carriage Returns
> and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance.
>
> Also, are they any great references for learning how to use Regular
> Expressions?
>
>


Ok, here is an example:
/////////////////////////////////////////////////////////////////////
var text = "Test\nWith\nSome\nCarriageReturns\rAnd\nLineFeeds \r\n";
alert(text);

var text2 = text.replace(/[\r\n]/g, "");
alert(text2);
/////////////////////////////////////////////////////////////////////

Here is what it is saying... Find a pattern with a single character of
\r (Carriage Return) or \n (New lines), and replace it with an empty
string. the /.../ slashes encapsulate the RegEx, and the g at the end
is a command to the RegEx reader to do it for all matches, not just the
first one.

In effect, all \r and \n characters are removed completely...

As for a reference for Regular Expressions, I know that OReily has a
grea book on them... I am not sure about online references. I haven't
needed a reference in so long, because once I learned them, I found
places to use them all the time... in editors, or in programing, so I
never forgot them. (That is my plug to encourage you to learn them)

Brian

 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      01-29-2004
"Brian Genisio" <(E-Mail Removed)> wrote in message
news:4018fde4$1@10.10.0.241...
> McKirahan wrote:
> > How would I use a regular expression to remove all trailing Carriage

Returns
> > and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance.
> >
> > Also, are they any great references for learning how to use Regular
> > Expressions?
> >
> >

>
> Ok, here is an example:
> /////////////////////////////////////////////////////////////////////
> var text = "Test\nWith\nSome\nCarriageReturns\rAnd\nLineFeeds \r\n";
> alert(text);
>
> var text2 = text.replace(/[\r\n]/g, "");
> alert(text2);
> /////////////////////////////////////////////////////////////////////
>
> In effect, all \r and \n characters are removed completely...



Thank you for your reply.

I had asked how to remove only trailing CrLf's not all of them.

I'm also interested in how to remove all leading CrLf's.

The next level would be to replace all embedded instances of 3 consecutive
CrLf's with 2.

The purpose of this is to clean up "textarea" input as it may contain
extraneous CrLf's.

P.S. I know the basics of Regular Expressions, I'm curious as where to
learn the advanced stuff; I'll look at O'Reilly.


 
Reply With Quote
 
McKirahan
Guest
Posts: n/a
 
      01-29-2004
> I had asked how to remove only trailing CrLf's not all of them.
>
> I'm also interested in how to remove all leading CrLf's.
>
> The next level would be to replace all embedded instances of 3 consecutive
> CrLf's with 2.
>
> The purpose of this is to clean up "textarea" input as it may contain
> extraneous CrLf's.



I posted this more detailed question ("RegExp removing extraneous CrLf's?")
at "microsoft.public.scripting.vbscript" and received the following solution
from "Steve Fulton":

With New RegExp
.Pattern = "^(?:\r\n)+|(?:\r\n)+$|((\r\n)\2)\2+"
.Global = True
.MultiLine = False
NewString = .Replace(OldString, "$1")
End With


Below is my JavaScript version of it:

<html>
<head>
<title>textarea.htm</title>
<script type="text/javascript">
function crlf() {
var form = document.forms[0];
var data = form.data.value;
var regx = new RegExp("^(?:\r\n)+|(?:\r\n)+$|((\r\n)\2)\2+");
regx.global = true;
regx.multiline = false;
var temp = data.replace(regx,"$1");
temp = temp.replace(regx,"$1");
alert(data.length + " : " + temp.length);
form.data.value = temp;
}
</script>
</head>
<body>
<form>
<textarea name="data" cols="50" rows="10"></textarea>
<br><input type="button" value="OK" onclick="crlf()">
</form>
</body>
</html>



Any idea why I have to add a second
temp = temp.replace(regx,"$1");
to get it to remove trailing CrLf's?
I'll ask "Steve Fulton"...


 
Reply With Quote
 
rh
Guest
Posts: n/a
 
      01-30-2004
"McKirahan" <(E-Mail Removed)> wrote:


<snip>
> Below is my JavaScript version of it:
>
> <html>
> <head>
> <title>textarea.htm</title>
> <script type="text/javascript">
> function crlf() {
> var form = document.forms[0];
> var data = form.data.value;
> var regx = new RegExp("^(?:\r\n)+|(?:\r\n)+$|((\r\n)\2)\2+");
> regx.global = true;
> regx.multiline = false;
> var temp = data.replace(regx,"$1");
> temp = temp.replace(regx,"$1");
> alert(data.length + " : " + temp.length);
> form.data.value = temp;
> }
> </script>
> </head>
> <body>
> <form>
> <textarea name="data" cols="50" rows="10"></textarea>
> <br><input type="button" value="OK" onclick="crlf()">
> </form>
> </body>
> </html>
>
>
>
> Any idea why I have to add a second
> temp = temp.replace(regx,"$1");
> to get it to remove trailing CrLf's?
> I'll ask "Steve Fulton"...


Most likely because, while "global" is a property of RegExps, it is
read-only. The way to set it is to provide it as an argument to the
RegExp constructor.

See
<url: http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/>

Also, your converted rendition has a problem in that it will not
remove the embedded cr/lf's. That's because escapes ("\"s) must be
doubled in a string in order that they not cause conversion of the
character following to a normalized string value. That's OK for "\r"
and "\n", but the \2 back-reference will not survive as such in the
generated regular expression.

When working with non-literal RegExp's, it's often a good idea to
print the RegExp following construction, e.g.,

alert(regx.toString());

to ensure it's formed as intended.

Also note that some browsers, e.g., Netscape, treat the replacement
string "$1" as a literal, rather than null, if it hasn't been assigned
a value during the pattern match execution at the time of
substitution.

And finally, the RegExp supplied reduces 3 or more \r\n's to 2, which
isn't what you stated in the request, but nonetheless may be what you
wanted.

../rh
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Remove trailing spaces in ecilpse pd Java 3 12-07-2007 12:32 PM
Remove trailing space in Open Office spreadsheets? Evan Platt Computer Support 1 08-28-2006 09:05 PM
Q: Regexp for fixed-length string with trailing whitespace andreas1234567@hotmail.com Perl Misc 3 09-09-2005 12:26 PM
remove trailing whitespace from string Donald Canton C++ 5 02-09-2004 04:39 PM



Advertisments