By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,599 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,599 IT Pros & Developers. It's quick & easy.

What should this replace function call do?

P: n/a
If I have a snippet of javascript code like this:

var str = "This is a snippet;
str.replace(/ */gi,' ');

what should the call to replace do? My actual tests show that it inserts
spaces between each letter... but this doesn't make sense to me. From the
regex, it looks like it should be replacing any occurrence of a space
followed by any character with a space, meaning that we should have
something like 'This s nippet'.
Please help me clarify this.

TIA,

--
Akin

aknak at aksoto dot idps dot co dot uk
Oct 2 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Epetruk" <no****@blackhole.com> writes:
If I have a snippet of javascript code like this:

var str = "This is a snippet;
str.replace(/ */gi,' ');

what should the call to replace do?
Replace all sequences of SPACE characters of length 0(!) or more with
a single space.
My actual tests show that it inserts spaces between each
letter...
Correct. Between each pair of letters is a (zero length) sequence of
space characters.
but this doesn't make sense to me. From the regex, it
looks like it should be replacing any occurrence of a space followed
by any character with a space,


No, "*" means "zero or more of the previous". What you are thinking of
would be / ./g, because "." matches everything (except a newline).

/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.'
Oct 2 '05 #2

P: n/a
Epetruk wrote:
If I have a snippet of javascript code like this:

var str = "This is a snippet;
str.replace(/ */gi,' ');

what should the call to replace do? My actual tests show that it inserts
spaces between each letter... but this doesn't make sense to me. From the
regex, it looks like it should be replacing any occurrence of a space
followed by any character with a space, meaning that we should have
something like 'This s nippet'.
Please help me clarify this.


When used in a regular expression, '*' replaces zero or more instances.
So it replaces zero or more instances of ' ' with ' '. It will turn:

'AA A' into 'A A A'

If you want to replace one or more spaces with one space, use '+'.

alert("This is a snippet".replace(/ +/gi,' '));
// --> This is a snippet

If you want to replace a space followed by any letter, then:

alert("This is a snippet".replace(/( [a-z])+/gi,' '));
// --> This s nippet

will do the trick (though I can't see any reason to do that).
--
Rob
Oct 2 '05 #3

P: n/a
Epetruk wrote:
If I have a snippet of javascript code like this:

var str = "This is a snippet; ^[1] str.replace(/ */gi,' '); ^^[2] what should the call to replace do?
Considering the undelimited string literal[1] above to be just a typo,
it eventually should do nothing since "as is" the result is not evaluated
in any way[2] ...
My actual tests show that it inserts spaces between each letter...


....; in case that is a typo as well, it replaces the empty string and
every sequence of space characters with a single space, i.e. includes
a space between (non-space) *characters* (not only letters), as the
others explained.
PointedEars
Oct 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.