473,320 Members | 2,109 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,320 software developers and data experts.

Bug in Safari Javascript?

When running the code below, all browsers are showing "Hello World" except Safari which cannot match
the regular expression. See also http://www.testabc.nl/safari/test.html

When I change [^A]*? into .*? or add a space at the end of the string Safari is working alright
again.

I guess this is a bug in Safari Javascript, or?

<html>
<head>

<script language=JavaScript type=text/javascript>

var reg_date = /(Hello[^A]*?)\s*$/;
var str = 'Start: Hello world';

function show()
{

if(str.search(reg_date) != -1) alert(RegExp.$1);
else alert ("No match...");
}

</script>

</head>

<body>

<h3>Javascript code:</h3>
<div>var reg_date = /(Hello[^A]*?)\s*$/;</div>
<div>var str = 'Start: Hello world';</div>
<div>if(str.search(reg_date) != -1) alert(RegExp.$1);</div>
<divelse alert ("No match...");</div>

<p><button onclick="show();">start</button></p>
</body>
</html>
Jul 10 '07 #1
13 2015
d d
Zwerfkat wrote:
When running the code below, all browsers are showing "Hello World" except Safari which cannot match
the regular expression. See also http://www.testabc.nl/safari/test.html
I guess this is a bug in Safari Javascript, or?
I also had a regular expression that worked in everything except Safari.
Don't worry though, Steve Jobs says it's the best browser in the world,
so it must be all the other browsers that are wrong.

It's garbage like Safari that means we have to sniff for browsers when
we really shouldn't need to.

I ended up having to do my replacements without regular expression. Oh,
and btw, my regular expression was about as easy as they get:

s=s.replace(/ /g,"_"); //replace spaces with underlines

~dd
Jul 10 '07 #2

"Zwerfkat" <f.********@inter.nl.netwrote in message
news:34*****************@altium.nl...
When running the code below, all browsers are showing "Hello World" except
Safari which cannot match the regular expression. See also
http://www.testabc.nl/safari/test.html
That seems doubtful. "Hello world" perhaps?
>
When I change [^A]*? into .*? or add a space at the end of the string
Safari is working alright again.
These work in my copy of Windows Safari Beta.

<script type="text/javascript">

var reg_date = /(Hello[^A]*)$/;
var str = 'Start: Hello world';

if(str.search(reg_date) != -1) alert(RegExp.$1);
else alert ("No match...");

reg_date = /(Hello[\x20]*[^\x20]*).*$/;
str = 'Start: Hello world this is a test';

if(str.search(reg_date) != -1) alert(RegExp.$1);
else alert ("No match...");

</script>

Yours didn't and I am not sure why. It seems like it should and my LCD test
browser (NS6.2) handled it properly. I do know that Windows Safari Beta is
still pretty buggy. In my experience, it displays NOSCRIPT content with
JavaScript enabled, its window doesn't size properly and it has some screwy
positioning problems. I tried reporting these bugs, but that button crashed
the browser when I tried it. They've got a lot of work to do.
Jul 10 '07 #3
Zwerfkat wrote:
When running the code below, all browsers are showing "Hello World" except Safari which cannot match
the regular expression. See also http://www.testabc.nl/safari/test.html

When I change [^A]*? into .*? or add a space at the end of the string Safari is working alright
again.

I guess this is a bug in Safari Javascript, or?

<html>
<head>

<script language=JavaScript type=text/javascript>

var reg_date = /(Hello[^A]*?)\s*$/;
I am no expert in regular expressions, however I think the use of ?
immediately after * is incorrect. It makes no sense to say one or more
repeats (?) of zero or more repeats ([^A]*).
If what you are after is a non-greedy version of a repeating pattern,
shift the ? outside the parenthesis and use:

/(Hello[^A]*)?\s*$/
which "works", or at least shows "Hello world" which is what you say
indicates "working".
--
Rob
"We shall not cease from exploration, and the end of all our
exploring will be to arrive where we started and know the
place for the first time." -- T. S. Eliot
Jul 10 '07 #4
d d wrote:
Zwerfkat wrote:
>When running the code below, all browsers are showing "Hello World"
except Safari which cannot match the regular expression. See also
http://www.testabc.nl/safari/test.html
I guess this is a bug in Safari Javascript, or?

I also had a regular expression that worked in everything except Safari.
Don't worry though, Steve Jobs says it's the best browser in the world,
so it must be all the other browsers that are wrong.
[...]
s=s.replace(/ /g,"_"); //replace spaces with underlines
In which version of Safari did that fail? It works in versions 2 and 3.
If you insist I'll test it in version 1, care to place a bet?
--
Rob
"We shall not cease from exploration, and the end of all our
exploring will be to arrive where we started and know the
place for the first time." -- T. S. Eliot
Jul 10 '07 #5
I am no expert in regular expressions, however I think the use of ? immediately after * is
incorrect. It makes no sense to say one or more repeats (?) of zero or more repeats ([^A]*).
It makes perfectly sense. The "?" means non-greedy of the repeating pattern [^A]*.

/(Hello[^A]*?)\s*$/

This RegExp is eating as little as possible non 'A' characters (non-greedy) after "Hello" until the
space(s) are reached at the end of the line. So in this example the result is everything after
"Hello" without any space(s) at the end.
If what you are after is a non-greedy version of a repeating pattern, shift the ? outside the
parenthesis and use:

/(Hello[^A]*)?\s*$/
This is not what I am after. Moreover, the result will be "Hello world " including the space at the
end and that's not what I had in mind.
which "works", or at least shows "Hello world" which is what you say indicates "working".

Jul 10 '07 #6
Zwerfkat wrote:
>I am no expert in regular expressions, however I think the use of ? immediately after * is
incorrect. It makes no sense to say one or more repeats (?) of zero or more repeats ([^A]*).

It makes perfectly sense. The "?" means non-greedy of the repeating pattern [^A]*.
Non-greedy of zero or more seems to me it should be zero matches, and
since there is other stuff between Hello and the spaces at the end of
the string, there should be no matches - maybe Safari it the only one
getting it right:

"? directly following a quantifier makes the quantifier
non-greedy (makes it match minimum instead of maximum
of the interval defined).

"E.g: /(.)*?/ matches nothing or '' in all strings."

<URL: http://www.javascriptkit.com/javatutors/redev2.shtml >

/(Hello[^A]*?)\s*$/

This RegExp is eating as little as possible non 'A' characters (non-greedy) after "Hello" until the
space(s) are reached at the end of the line. So in this example the result is everything after
"Hello" without any space(s) at the end.
>If what you are after is a non-greedy version of a repeating pattern, shift the ? outside the
parenthesis and use:

/(Hello[^A]*)?\s*$/

This is not what I am after. Moreover, the result will be "Hello world " including the space at the
end and that's not what I had in mind.
Did you test that? I did. Neither Firefox, Safari or Opera include any
spaces after world - the ? is immediately before \s which seems to be
what you wanted. I haven't tested IE.
--
Rob
"We shall not cease from exploration, and the end of all our
exploring will be to arrive where we started and know the
place for the first time." -- T. S. Eliot
Jul 10 '07 #7
d d
RobG wrote:
>I also had a regular expression that worked in everything except
Safari.
s=s.replace(/ /g,"_"); //replace spaces with underlines

In which version of Safari did that fail? It works in versions 2 and 3.
If you insist I'll test it in version 1, care to place a bet?
It was Safari 1.3 and the code that it was part of was dynamically
written into the parent page from inside an iframe using createElement.
I can well believe that in normal inline code it works fine.

~dd
Jul 10 '07 #8
d d wrote:
RobG wrote:
>>I also had a regular expression that worked in everything except Safari.
s=s.replace(/ /g,"_"); //replace spaces with underlines

In which version of Safari did that fail? It works in versions 2 and
3. If you insist I'll test it in version 1, care to place a bet?

It was Safari 1.3 and the code that it was part of was dynamically
written into the parent page from inside an iframe using createElement.
I can well believe that in normal inline code it works fine.
It's probably a moot point, but that situation is the prime reason you
should use the RegExp constructor.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jul 10 '07 #9
On Jul 10, 8:32 am, RobG <r...@iinet.net.auwrote:
Zwerfkat wrote:
<..>
If what you are after is a non-greedy version of a repeating pattern, shift the ? outside the
parenthesis and use:
/(Hello[^A]*)?\s*$/
This is not what I am after. Moreover, the result will be "Hello world " including the space at the
end and that's not what I had in mind.

Did you test that? I did. Neither Firefox, Safari or Opera include any
spaces after world - the ? is immediately before \s which seems to be
what you wanted. I haven't tested IE.
You may want to try your tests again.

The [^A]* allows the regular expression to take as many as possible of
anything other than "A", and that includes spaces. Since the "\s" is
quantified by "*", meaning none need to be found at the end,
processing of [^A]* can continue to the end of the string, and the
regular expression processing can return success, because the ?
quantifier allows (either zero or) one of these. So, in this case, any
trailing blanks should be found within RegExp.$1, where a right
trimmed string is desired.

There's another problem. Because you've introduced a zero or one test,
your expression will return success on a null string or a string that
is all blanks. It should fail if there is no "Hello" with zero or more
trailing blanks.

On the other hand, if [^A]*? is used, the processing will attempt to
succeed with the minimum taken by the [^A]'s. The first point at which
it will succeed is when there are zero or more blanks remaining,
because the \s* will cover (eat) all of those. Therefore, in the case
of the OP's regular expression, RegExp.$1 should not contain any
trailing blanks.

--
../rh

Jul 11 '07 #10
On Jul 11, 12:22 pm, ron.h.h...@gmail.com wrote:
On Jul 10, 8:32 am, RobG <r...@iinet.net.auwrote:
Zwerfkat wrote:
<..>
>If what you are after is a non-greedy version of a repeating pattern, shift the ? outside the
>parenthesis and use:
> /(Hello[^A]*)?\s*$/
This is not what I am after. Moreover, the result will be "Hello world " including the space at the
end and that's not what I had in mind.
Did you test that? I did. Neither Firefox, Safari or Opera include any
spaces after world - the ? is immediately before \s which seems to be
what you wanted. I haven't tested IE.

You may want to try your tests again.
I can do them a thousand times, but computers being what they are,
given the same input and processing instructions they keep giving the
same result. :-)

The next question is whether the tests were sufficient or not.

The [^A]* allows the regular expression to take as many as possible of
anything other than "A", and that includes spaces. Since the "\s" is
quantified by "*", meaning none need to be found at the end,
processing of [^A]* can continue to the end of the string, and the
regular expression processing can return success, because the ?
quantifier allows (either zero or) one of these. So, in this case, any
trailing blanks should be found within RegExp.$1, where a right
trimmed string is desired.
The OP's test case didn't inlcude any spaces at the end of the string,
I should have modified the test case but didn't.

There's another problem. Because you've introduced a zero or one test,
your expression will return success on a null string or a string that
is all blanks. It should fail if there is no "Hello" with zero or more
trailing blanks.
Yes.

On the other hand, if [^A]*? is used, the processing will attempt to
succeed with the minimum taken by the [^A]'s. The first point at which
it will succeed is when there are zero or more blanks remaining,
because the \s* will cover (eat) all of those. Therefore, in the case
of the OP's regular expression, RegExp.$1 should not contain any
trailing blanks.
Yes.
--
Rob

Jul 11 '07 #11
On Jul 10, 8:17 pm, RobG <r...@iinet.net.auwrote:
On Jul 11, 12:22 pm, ron.h.h...@gmail.com wrote:
On Jul 10, 8:32 am, RobG <r...@iinet.net.auwrote:
Zwerfkat wrote:
<..>
If what you are after is a non-greedy version of a repeating pattern, shift the ? outside the
parenthesis and use:
/(Hello[^A]*)?\s*$/
This is not what I am after. Moreover, the result will be "Hello world " including the space at the
end and that's not what I had in mind.
Did you test that? I did. Neither Firefox, Safari or Opera include any
spaces after world - the ? is immediately before \s which seems to be
what you wanted. I haven't tested IE.
You may want to try your tests again.

I can do them a thousand times, but computers being what they are,
given the same input and processing instructions they keep giving the
same result. :-)
Even so, sometimes mistakes are made in reading test results,
especially when spaces are involved. But I see through your further
explanation, the problem was ... they weren't. ;-)

--
../rh

Jul 11 '07 #12
On Jul 11, 1:53 pm, ron.h.h...@gmail.com wrote:
On Jul 10, 8:17 pm, RobG <r...@iinet.net.auwrote:
On Jul 11, 12:22 pm, ron.h.h...@gmail.com wrote:
On Jul 10, 8:32 am, RobG <r...@iinet.net.auwrote:
Zwerfkat wrote:
<..>
>If what you are after is a non-greedy version of a repeating pattern, shift the ? outside the
>parenthesis and use:
> /(Hello[^A]*)?\s*$/
This is not what I am after. Moreover, the result will be "Hello world " including the space at the
end and that's not what I had in mind.
Did you test that? I did. Neither Firefox, Safari or Opera include any
spaces after world - the ? is immediately before \s which seems to be
what you wanted. I haven't tested IE.
You may want to try your tests again.
I can do them a thousand times, but computers being what they are,
given the same input and processing instructions they keep giving the
same result. :-)

Even so, sometimes mistakes are made in reading test results,
especially when spaces are involved. But I see through your further
explanation, the problem was ... they weren't. ;-)
Yes - GI, GI. :-x

--
Rob

Jul 11 '07 #13
d d
-Lost wrote:
d d wrote:
>s=s.replace(/ /g,"_"); //replace spaces with underlines
It was Safari 1.3 and the code that it was part of was dynamically
written into the parent page from inside an iframe using
createElement. I can well believe that in normal inline code it works
fine.

It's probably a moot point, but that situation is the prime reason you
should use the RegExp constructor.
That might have worked for me. Too late now, my solution worked and is
already deployed. I was lucky that I wasn't trying to do a complex
regular expression.

~dd
Jul 11 '07 #14

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

Similar topics

1
by: Felipe Gasper | last post by:
Hello, Does anyone know if KHTML/Safari just doesn't support code such as: ---------- HTMLInputElement.prototype.toggle = function(enabled) { //some really spiffy code } ----------
4
by: Bernard | last post by:
Hi, I am suddenly getting Safari script errors with the following user agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.2 (KHTML, like Gecko) Safari/125.8 In a...
2
by: drew197 | last post by:
I am a newbie. This is someone else's code which I have to modify to work in Mozilla and Safari. The changes I made so far have allowed it to work in Mozilla but not Safari. It seems to be...
1
by: bruceeanderson | last post by:
This script does not run on MAC OS 10 w/Safari, but it does run under XP w/IE. This is a report setup screen. The user selects a customer from a select window and picks a date range from a popup...
2
by: darren | last post by:
I have a small Javascript problem with that mutch love web browser safari, I tested the code on all other browsers PC (Win) and Linux and IE on the mac and it seams to work ok, but for some reason...
14
by: maidane | last post by:
Hi, I got the following situation using the AC_Quicktime library. Apple recommend to use a Javascript call to generate the OBJECT tag (http://developer.apple.com/internet/ieembedprep.html). I...
7
by: Tom | last post by:
I have an oo-type javascript program running perfectly on IE 6.0+, FF 1.5+, and Opera 7+ on Windows 98+, Linux (RH 9, FC 6), and Mac OS X. 4. As usual, the Safari browser is not working correctly,...
0
by: atencorps | last post by:
Hello I have the following code but need some help on it. The idea of the code is the main sections ie Service Management are viewable when the page is loaded and by clicking on the main...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.