By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

Smiple Script Question

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
/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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.