473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

RegExp for remove all trailing CrLf's?

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
4 24702
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
"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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: B. | last post by:
Hello, I've got the following problem: Suppose you have the strings contains: "xxxx aaa { 111, 222, 333} bbb {111, 222,333} yyyy" "xxxx aaa {1112, 2223, 3334} bbb {11112, 22223,33334,44445}...
10
by: Andrew DeFaria | last post by:
I was reading my O'Reilly JavaScript The Definitive Guide when I came across RegExp and thought I could tighten up my JavaScript code that checks for a valid email address. Why does the following...
5
by: McKirahan | last post by:
I'd like to use regular expressions to remove extraneous Carriage Return Line Feeds (CrLf) from a textarea before the form is submitted. I'd like to remove all trailing CrLf's and convert all...
7
by: iannorton | last post by:
Hi, I'm trying to write a RegExp that will return search an array and return the closest 10 matches, for example, if i entered "Hel" and my array contained "Hello", "Hell", it would return Hello...
7
by: Bosconian | last post by:
I know that str.replace(/^\s+|\s+$/g,''); will trim a string of space, but what about removing extra spaces from the middle? Where "hello world"
3
by: Paul | last post by:
Hi, My RichTextBox has multiple lines of text. Most of the lines unfortunately end with a space. Is it possible to replace the space and NewLine/Line Feed with just the NewLine/LineFeed? So...
3
by: jgarrard | last post by:
Hi, I have an array of strings which are regular expressions in the PERL syntax (ie / / delimeters). I wish to create a RegExp in order to do some useful work, but am stuck for a way of...
4
by: lihao0129 | last post by:
Hi, folks: I recently went through a strange problem with my Javascript code, say: I have a string variable which are from a 'textarea' element and I want to remove the trailing newlines inside...
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.