473,387 Members | 1,420 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,387 software developers and data experts.

Smiple Script Question

Could someone tell me what's wrong with this code?
It's supposed to produce an alert box with "George" replacing "Paul" and
"Paula", but I'm only replacing the first name.

var myString = "Paul, Paula, Pauline";
var myRegExp = /Paula?/;
myString = myString.replace(myRegExp, "George");
alert(myString);

Is the 'question mark' the right character to use for this application?
Oct 5 '05 #1
2 1028
/Paula/ suffices.

? means: zero or one.
Applied in the way you do, that does not apply to the whole term Paula,
but only to its last LETTER: paulA

So, it means:

look for whatever that includes Paul
and that is followed either one or zero times by: "a".

First match that satisfies the condition is precisely Paul.

If you would have meant the ? quantifier to apply to the whole word,
sorround with round brackets the whole word it is meant to flag :
/(Paula)?/

YET beware that ALSO then it won't work the expected way: in fact, it
now means:

replace "Paula" IF it appears one or ZERO times

It would thus replace the first occurrence of the string where Paula is
not found, namely the vvery beginning of it and would yield:

GeorgePaul, Paula, Pauline

I hope this clarifies why it couldn't work.

Persoanlly, I'd suggest:

1) do not initialize like a regexp: place it right inside the replace
method:
myString.replace(/Paula/, "George");
2) You may want wehter to make iot global and case sensitive:
myString.replace(/Paula/gi, "George");
3) You may want to add a boundary delimiters (\b) before and after
Paula so to be sure it does not replace paula if by chance such a
string is in between a word (unlikely for a term like 'Paula', I
agree):
myString.replace(/\bPaula\b/gi, "George");

So your options are either (first preferred):
/\bPaula\b/gi
or just
/Paula/

ciao
Alberto
http://www.unitedscripters.com/

Oct 6 '05 #2
On 06/10/2005 00:27, ommadawn wrote:

[snip]
It's supposed to produce an alert box with "George" replacing "Paul" and
"Paula", but I'm only replacing the first name.

var myString = "Paul, Paula, Pauline";
var myRegExp = /Paula?/;
myString = myString.replace(myRegExp, "George");
alert(myString);

Is the 'question mark' the right character to use for this application?


Yes as it marks the 'a' as optional, allowing the regular expression to
match both names.

The problem is that it will only match the first occurrence. You need to
include the global flag. However, you will still have problems as it
would then match the 'Paul' in 'Pauline'. It needs to distinguish
between whole and partial words. This can be achieved using the \b
assertion which tests for a non-word character (not one that matches
[A-Za-z0-9_]), or the beginning or end of a string:

myString.replace(/\bPaula?\b/g, 'George');

So, 'Paul' is matched because it's at the start of the string and is
followed by a comma. 'Paula' is matched because it's preceded by a space
and followed by another comma. The 'Paul' in 'Pauline' is not matched
because though the preceding space isn't a word character, the 'i' is.

Hope that helps,
Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 6 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: el_roachmeister | last post by:
I have a 4000 line php cgi script that is not memory or cpu intensive. It runs in 1 second. Now I am getting a lot of customer requests to add in new features. My question is if there is a point...
2
by: Øystein Johansen | last post by:
Hi, (This question may be stupid, but I just can't find the answer...) How can I make the browser downlaod a cgi script from /cgi-bin/. Obviously I can't make a link to it with...
0
by: Mark Griffin | last post by:
Hi... I am trying to convert a simple FAQ script to access mysql db rather than the Access db it was designed for. The originators of the script offer no support at all. I have managed to set...
1
by: Kevin Potter | last post by:
We have an application that has been running on IIS4 and IIS5 for quite some time, without problem We're now migrating to IIS6 (windows/2003), and have run into a what might? be a Javascipt...
0
by: ZMan | last post by:
Scenario: This is about debugging server side scripts that make calls to middle-tier business DLLs. The server side scripts are legacy ASP 3.0 pages, and the DLLs are managed DLLs...
2
by: Kingo | last post by:
Hi, First, please forgive my terrible knowledge of JS! I haven't used it in years. I am trying to create a Help page where a new question is on each line, and when clicking on the question,...
19
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
4
dreamcatcher
by: dreamcatcher | last post by:
Hi everyone, I'm a frontend designer starting to learn about getting forms to work. What I'm after is a script that I can learn from and get working on a site relatively quickly. Something that...
3
by: Alan Isaac | last post by:
This is a simple question about actual practice. I just want to know how you (yes you) are approaching this problem. The problem: What is the recommended packaging of demo scripts or test...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.