"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