What should this replace function call do?

E

Epetruk

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,
 
L

Lasse Reichstein Nielsen

Epetruk said:
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
 
R

RobG

Epetruk said:
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).
 
T

Thomas 'PointedEars' Lahn

Epetruk said:
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
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top