How-to: JavaScript trim() and normalize-space() functions

A

Alex Vassiliev

Hi all.

Just wanted to share two handy RegEx expressions to strips leading and
trailing white-space from a string, and to replace all repeated spaces,
newlines and tabs with a single space.

* JavaScript example:

String.prototype.trim = function() {
// Strip leading and trailing white-space
return this.replace(/^\s*|\s*$/g, "");
}

String.prototype.normalize_space = function() {
// Replace repeated spaces, newlines and tabs with a single space
return this.replace(/^\s*|\s(?=\s)|\s*$/g, "");
}

" one \t two three \n ".trim(); // --> "one \t two three"
" one \t two three \n ".normalize_space(); // --> "one two three"


Enjoy,
Alex Vassiliev (New Zealand)
 
E

Evertjan.

Alex Vassiliev wrote on 29 sep 2005 in comp.lang.javascript:
String.prototype.trim = function() {
// Strip leading and trailing white-space
return this.replace(/^\s*|\s*$/g, "");
}

Nice one, Alex.

To have them all [the recurses are a variation]:

<script type='text/javascript'>

String.prototype.trim = function(x) {
if (x=='left')
return this.replace(/^\s*/,'');
if (x=='right')
return this.replace(/\s*$/,'');
if (x=='normalize')
return this.replace(/\s{2,}/g,' ').trim();

return this.trim('left').trim('right');
}

test = ' \n blah \n blah \n '
alert('x'+test+'x')
alert('x'+test.trim('left')+'x')
alert('x'+test.trim('right')+'x')
alert('x'+test.trim('both')+'x')
alert('x'+test.trim('normalize')+'x')
alert('x'+test.trim()+'x')

</script>
 
M

Michael Winter

On 29/09/2005 09:08, commercial wrote:

[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

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
 
A

Alex Vassiliev

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
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Wed, 28 Sep 2005 18:28:42, seen in
Alex Vassiliev said:
String.prototype.normalize_space = function() {
// Replace repeated spaces, newlines and tabs with a single
space
return this.replace(/^\s*|\s(?=\s)|\s*$/g, "");
}

That won't work for me.

Otherwise, I might have used it in my presently-crude paragraph-packer.

I much dislike reading articles,
usually
press statements or similar, in
which the
layout is like this forced
example
(but wider).

So I added a "Pack" button to <URL:http://www.merlyn.demon.co.uk/js-
quick.htm>; its code is still crude.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top