RegExp for remove all trailing CrLf's?

M

McKirahan

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?
 
B

Brian Genisio

McKirahan said:
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
 
M

McKirahan

Brian Genisio said:
McKirahan said:
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.
 
M

McKirahan

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"...
 
R

rh

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top