469,623 Members | 985 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

What should this replace function call do?

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
3 1517
"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
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
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.

Similar topics

28 posts views Thread by David MacQuigg | last post: by
5 posts views Thread by K.Simon | last post: by
4 posts views Thread by F. Da Costa | last post: by
10 posts views Thread by linq936 | last post: by
5 posts views Thread by djc | last post: by
3 posts views Thread by Hvid Hat | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.