473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1668
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
12
by: Barnes | last post by:
Does anyone know of a good way to use the JavaScript string.replace() method in an ASP form? Here is the scenario: I have a form that cannot accept apostrophes. I want to use the replace() so...
5
by: K.Simon | last post by:
Hello, it's very often neccessary to replace strings or a single character in my stylesheets. My solution looks awful and very long. Now i thought to solve this with an array like structure but...
4
by: F. Da Costa | last post by:
Hi, I was wondering whether someone could enlighten me as to the reason why the slice does not work in IE when the arr is passed in properly. Checked the values in the srcArr and they are...
10
by: linq936 | last post by:
Hi, I have many assert() call in my code, now I am considering to replace them with exception. The reason I want to do this change is that with the program going bigger and bigger, it is hard to...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}")...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
1
by: coolami4u | last post by:
I need a program that simulates the search-and-replace operation in a text editor. The program is to have only three function calls in main. The first function prompts the user to type a string of...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.