473,771 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Remove extra CrLf's from textarea

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.Bod y.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\%0 A\%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
5 16069
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
"Andrew Urquhart" <re***@website. in.sig> wrote in message
news:5nq2c.1680 $re1.1203@newsf e1-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
"McKirahan" <Ne**@McKirahan .com> wrote in message
news:juq2c.1917 09$uV3.791436@a ttbi_s51...
"Andrew Urquhart" <re***@website. in.sig> wrote in message
news:5nq2c.1680 $re1.1203@newsf e1-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
McKirahan wrote:
"McKirahan" <Ne**@McKirahan .com> wrote in message
news:juq2c.1917 09$uV3.791436@a ttbi_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
"Andrew Urquhart" <re***@website. in.sig> wrote in message
news:U%q2c.2496 $54.1875@newsfe 1-win...
McKirahan wrote:
"McKirahan" <Ne**@McKirahan .com> wrote in message
news:juq2c.1917 09$uV3.791436@a ttbi_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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
13249
by: Jon LaRosa | last post by:
I'm am trying to remove multiple line breaks ("\n"). Here's my code: $imageStr = trim($_POST); $imageStr = str_replace("\n\n", "\n", $imageStr); ---------- $_POST is a textarea field, and I am trying to remove extra hard returns that a user types into the text area.
7
23936
by: Voetleuce en fênsievry | last post by:
Hello everyone. I'm not a JavaScript author myself, but I'm looking for a method to remove duplicate words from a piece of text. This text would presumably be pasted into a text box. I have, for example, a list of town names, but there are hundreds of duplicates, like: "Aberdeen Aberdeen Aberdeen Edinburg Edinburg Inverness etc."
4
24771
by: McKirahan | last post by:
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?
1
2232
by: Jack Wright | last post by:
Dear All, In a multiline field if I enter the following way: Mangesh Anant Kodilkar There is a carriage return after each word. Now when I press save,it loses the CRLF characters. This is not correct. Even after the page is reloaded the CRLF's are lost. I get this problem on my PC (XP), on Win (2000), on Win (NT & 98). So I
3
5839
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 in essence just removing the trailing space from each line?
4
5978
by: Franky | last post by:
What I want to do is delete the last line in a RichTextBox. The RichTextBox has a ReadOnly property called lines that seems like it might help but I cant figure out how to use it. Well, the question is what is the easiest way to remove the last line in a RichTextBox? Thanks
4
22348
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 the string. I am using something like the following: var txt = textarea_element.value.replace(/\n*$/, ''); But this replaced only the last newline(by changing '' to 'K', and
1
2394
by: Hunter | last post by:
I am writing a script that needs to send some emails. And I've used smtplib in the past and it is pretty easy. But I thought, gee it would be easier if I could just call it as a function, passing the from, to, subject, and message text. So I wrote it up as a function and it sort of works, but I get a weird error. When it runs it inserts a "\t" tab character before each item during the send portion (which I can see when I turn on...
9
2144
by: qiqinuinaifen128 | last post by:
Hi Pro, I have a question i have an textarea for user to key in and a button for user to save what they key in into the textarea. if i key in '"\ in to textarea and i press the button save the file, the file is not shown '"\, it shows \'\"\\, Why the file was added in this symbol \ inside my file? This will not happen if i didn't key in these three symbols.
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.