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

Remove extra CrLf's from textarea

P: n/a
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 instances of 3
consectutive CrLf's to just 2.

I first escape() the textarea and match "%0D%0A" (i.e. CrLf).

Here's what I've been testing; watch for word-wrap:

<html>
<head>
<title>fix_crlf.htm</title>
<script type="text/javascript">
function fix() {
var form = document.forms[0];
var body = escape(form.Body.value);
// try to remove all trailing CrLf's
body = body.replace(/\%0D\%0A$/g,"");
// try to convert all 3 CrLf's to 2
body = body.replace(/\%0D\%0A\%0D\%0A\%0D\%0A/g,"\%0D\%0A\%0D\%0A");
body = unescape(body);
// update the form field to see if it worked -- NOT!
form.Body.value = body;
}
</script>
</head>
<body>
<form>
<textarea name="Body" rows="30"></textarea>
<input type="button" value="fix()" onclick="fix()">
</form>
</body>
</html>
I thought the "g" would handle multiple instances (of both cases) or do I
have to do a "for" loop?

Can anyone advise me? Thanks in advance.

Note: JavaScript will be enabled as this is for a browser-based application
that requires it.
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
McKirahan wrote:
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 instances of 3
consectutive CrLf's to just 2. <snip />

strData = strData.replace(/(\r\n\s*){2,}/g, "\r\n\r\n");

The above finds 2 or more CrLfs with optional whitespace between them
and replaces it all with 2 CrLfs. If you're not bothered about the
chance of whitespace then:

strData = strData.replace(/(\r\n){3,}/g, "\r\n\r\n");

Note: JavaScript will be enabled as this is for a browser-based
application that requires it.


For robustness do it after the form is submitted, i.e. on the server
--
Andrew Urquhart
- FAQ: http://jibbering.com/faq
- Archive: http://groups.google.com/groups?grou...ang.javascript
- Reply: www.andrewu.co.uk/about/contact/
Jul 20 '05 #2

P: n/a
"Andrew Urquhart" <re***@website.in.sig> wrote in message
news:5nq2c.1680$re1.1203@newsfe1-win...
McKirahan wrote:
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 instances of 3
consectutive CrLf's to just 2.

<snip />

strData = strData.replace(/(\r\n\s*){2,}/g, "\r\n\r\n");

The above finds 2 or more CrLfs with optional whitespace between them
and replaces it all with 2 CrLfs. If you're not bothered about the
chance of whitespace then:

strData = strData.replace(/(\r\n){3,}/g, "\r\n\r\n");

Thank you very much for your reply.

But how would I remove all trailing (and leading) CrLf's?

Thanks again.
Jul 20 '05 #3

P: n/a
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:juq2c.191709$uV3.791436@attbi_s51...
"Andrew Urquhart" <re***@website.in.sig> wrote in message
news:5nq2c.1680$re1.1203@newsfe1-win...
McKirahan wrote:
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 instances of 3
consectutive CrLf's to just 2.

<snip />

strData = strData.replace(/(\r\n\s*){2,}/g, "\r\n\r\n");

The above finds 2 or more CrLfs with optional whitespace between them
and replaces it all with 2 CrLfs. If you're not bothered about the
chance of whitespace then:

strData = strData.replace(/(\r\n){3,}/g, "\r\n\r\n");

Thank you very much for your reply.

But how would I remove all trailing (and leading) CrLf's?

Thanks again.

Perhaps it would be?

strData = strData.replace(/^(\r\n\s*)/g, "");
strData = strData.replace(/(\r\n\s*)$/g, "");
Jul 20 '05 #4

P: n/a
McKirahan wrote:
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:juq2c.191709$uV3.791436@attbi_s51...
But how would I remove all trailing (and leading) CrLf's?


Perhaps it would be?

strData = strData.replace(/^(\r\n\s*)/g, "");
strData = strData.replace(/(\r\n\s*)$/g, "");


strData = strData.replace(/^[\r\n]+|[\r\n]+$/g, "");
--
Andrew Urquhart
- FAQ: http://jibbering.com/faq
- Archive: http://groups.google.com/groups?grou...ang.javascript
- Reply: www.andrewu.co.uk/about/contact/
Jul 20 '05 #5

P: n/a
"Andrew Urquhart" <re***@website.in.sig> wrote in message
news:U%q2c.2496$54.1875@newsfe1-win...
McKirahan wrote:
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:juq2c.191709$uV3.791436@attbi_s51...
But how would I remove all trailing (and leading) CrLf's?


Perhaps it would be?

strData = strData.replace(/^(\r\n\s*)/g, "");
strData = strData.replace(/(\r\n\s*)$/g, "");


strData = strData.replace(/^[\r\n]+|[\r\n]+$/g, "");
--
Andrew Urquhart
- FAQ: http://jibbering.com/faq
- Archive: http://groups.google.com/groups?grou...ang.javascript
- Reply: www.andrewu.co.uk/about/contact/

Thanks, Andrew.

I recently bought a copy of O'Reilly's "Mastering Regular Expressions" which
I'm wading through; someday I'll be self-sufficient (maybe).
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.