By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

RegExp for remove all trailing CrLf's?

P: n/a
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?
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Jul 20 '05 #2

P: n/a
"Brian Genisio" <Br**********@yahoo.com> wrote in message
news:40********@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.
Jul 20 '05 #3

P: n/a
> 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"...
Jul 20 '05 #4

P: n/a
rh
"McKirahan" <Ne**@McKirahan.com> 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
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.