HighDots Forums  

What should this replace function call do?

Javascript JavaScript language (comp.lang.javascript)


Discuss What should this replace function call do? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Epetruk
 
Posts: n/a

Default What should this replace function call do? - 10-01-2005 , 09:04 PM






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



Reply With Quote
  #2  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: What should this replace function call do? - 10-01-2005 , 09:49 PM






"Epetruk" <nobody (AT) blackhole (DOT) com> writes:

Quote:
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.

Quote:
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.

Quote:
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 - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #3  
Old   
RobG
 
Posts: n/a

Default Re: What should this replace function call do? - 10-01-2005 , 09:58 PM



Epetruk wrote:
Quote:
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


Reply With Quote
  #4  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: What should this replace function call do? - 10-16-2005 , 03:08 PM



Epetruk wrote:

Quote:
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] ...

Quote:
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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.