"Cylix" <cy*******@gmail.com> writes:
further example:
I like eating very much
.
How can I using string.replace(regEx, '') and string.replace(regEx, '
') to have the result:
I like eating very much.
That's a more specific wish. Not only do you want to convert repeated
spaces to single ones, you also want to remove any space between words
and following punctuation, and all initial (and probably terminal)
spaces.
Might as well do it in three goes first:
string.replace(/^\s+|\s+$/g,"")
.replace(/\b\s+([^\w\s])/,"$1")
.replace(/\s+/," ")
The first replace removes initial and terminal whitespace.
The second replace removes spaces between a word boundary and
a non-word/non-space character.
The third replace converts single spaces to multiple spaces.
You can probably make a single regexp that does it all, but you'll
have to be inventive (and probably use advanced features like
lookahead) since you will want to replace with different strings
in each of the three cases.
But here is one attempt:
/^\s+|\s+$|\b\s+(?=[^\w\s])|\s+(?=\s)/g
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'