M
McKirahan
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.
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.