> Hi all.
>
[quoted text clipped - 19 lines]
> Enjoy,
> Alex Vassiliev (New Zealand)
[Corrected top-post]
[snip]
>> String.prototype.trim = function() {
>> // Strip leading and trailing white-space
>> return this.replace(/^\s*|\s*$/g, "");
>> }
Incidentally, this already exists in the group FAQ
(<URL:http://www.jibbering.com/faq/#FAQ4_16>).
>> String.prototype.normalize_space = function() {
>> // Replace repeated spaces, newlines and tabs with a single space
>> return this.replace(/^\s*|\s(?=\s)|\s*$/g, "");
>> }
Useful as they are, lookahead assertions aren't available in all
browsers. Unless you're happy compromising execution by raising syntax
errors, they should be avoided:
String.prototype.normalise = function() {
return this.replace(/^\s+|\s+$/g, '').replace(/\s{2,}/g, ' ');
};
One thing that needs consideration is behaviour with mixed whitespace.
Your regular expression saves the last whitespace character in a
sequence. So, an earlier character could have been a new line, but might
be replaced by a space. Conversely, the sequence could have been all
spaces with the exception of a final new line, and it's that character
that is saved.
By comparison, the alternative above would always replace a whitespace
sequence with a single space.
[snip]
> you will not even get there on an OS handling basis
If you do insist on posting, will you at least make sense. And don't
top-post.
> you are a lonely motherf*er
However, if you're going to behave like that, could you refrain from
posting at all.
Mike

Signature
Michael Winter
Prefix subject with [News] before replying by e-mail.
Alex Vassiliev - 29 Sep 2005 22:00 GMT
>One thing that needs consideration is behaviour with mixed whitespace.
>Your regular expression saves the last whitespace character in a
>sequence. So, an earlier character could have been a new line, but might
>be replaced by a space. Conversely, the sequence could have been all
>spaces with the exception of a final new line, and it's that character
>that is saved.
Good one! I haven't thought about that... It is definitely a bug.
Thank you Michael