473,548 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why this RegExp doesn't work

Hi

I am wondering why I couldn't get what I want in the following 3 cases
of re:

(A)

var p=/([a-zA-Z]+-?[a-zA-Z]+):([a-zA-Z0-9]+)/g
p.exec("style=' font-size:12'")
--[font-size:12,font-size,12] // expected

(B)

p.exec("style=' font-size:12;border-color:red'")
--[border-color:red,borde r-color,red]
// expected: [font-size:12,font-size,12,
border-color:red,borde r-color,red]

(C) Note the pattern below is the same as that above:

/([a-zA-Z]+-?[a-zA-Z]+):([a-zA-Z0-9]+)/g.exec("style=' font-size:12;border-color:red'")
--[font-size:12,font-size,12]

I expected to get both font-size and border-color reported. However, in
case (B) only border-color is reported, and in case (C) only font-size
reported. The weirdest thing is that (B) and (C) are only different in
how it is used -- in (B) the pattern is assigned to a variable before
use, in (C) the pattern is used directly.

Nov 3 '06 #1
6 2260
runsun pan wrote on 03 nov 2006 in comp.lang.javas cript:
var p=/([a-zA-Z]+-?[a-zA-Z]+):([a-zA-Z0-9]+)/g

p.exec("style=' font-size:12;border-color:red'")
--[border-color:red,borde r-color,red]
// expected: [font-size:12,font-size,12,
border-color:red,borde r-color,red]
try this:

<script type='text/javascript'>

var p=/[a-z]+-[a-z]+:[a-z0-9]+/gi
alert( p.exec("style=' font-size:12; border-color:red'") )
alert( p.exec("style=' font-size:12; border-color:red'") )

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 3 '06 #2

Evertjan. wrote:
try this:

<script type='text/javascript'>

var p=/[a-z]+-[a-z]+:[a-z0-9]+/gi
alert( p.exec("style=' font-size:12; border-color:red'") )
alert( p.exec("style=' font-size:12; border-color:red'") )

</script>
They give:
--[font-size:12] // first alert
--[border-color:red] // 2nd alert

This doesn't solve my questions. What I want is to solve it in one try.
I thought that's what the /g is for. If you have to run once more for
each match inside of the string, then, doesn't that mean that the /g
(global flag) is useless here ?

Nov 3 '06 #3

runsun pan написав:
This doesn't solve my questions. What I want is to solve it in one try.
I thought that's what the /g is for. If you have to run once more for
each match inside of the string, then, doesn't that mean that the /g
(global flag) is useless here ?
One more version:

var p=/[a-zA-Z]+-?[a-zA-Z]+:[a-zA-Z0-9]+/g
var res = "style='fon t-size:12;border-color:red'".mat ch(p);
for(var i=0; i<res.length; i++)
{
alert(res[i]+","+res[i].split(":"));
}

Nov 3 '06 #4
This doesn't solve my questions. What I want is to solve it in one try.
I thought that's what the /g is for. If you have to run once more for
each match inside of the string, then, doesn't that mean that the /g
(global flag) is useless here ?
It seems you might need to read up on perl regex.

Basically the /g means that it works on the whole string. So for
example if you were looking for this
/hello/g
and you have the string:
hello there this is a hello statement

then the /g would match both the first and second hellos. Normally the
regex will stop after it matches the condition.

Likewise to get the value in the array you want add () arround the
sections that you wish to seperate out.

For example you have:

font-size: 12px;
/([a-z]*?-[a-z]*?):.([0-9]*)px/
That will return on blah.match(rege x).toSource();
[font-size: 12px, font-size, 12]

Or there abouts, havent tested it. The () adds that section of the
regex element in the string to matches[1 .. N]

Make sense?

Nov 3 '06 #5

runsun pan wrote:
Evertjan. wrote:
try this:

<script type='text/javascript'>

var p=/[a-z]+-[a-z]+:[a-z0-9]+/gi
alert( p.exec("style=' font-size:12; border-color:red'") )
alert( p.exec("style=' font-size:12; border-color:red'") )

</script>

They give:
--[font-size:12] // first alert
--[border-color:red] // 2nd alert

This doesn't solve my questions. What I want is to solve it in one try.
I thought that's what the /g is for. If you have to run once more for
each match inside of the string, then, doesn't that mean that the /g
(global flag) is useless here ?
Wanna do everything with one command and without cycles? ;)

var found=[]
"style='fon t-size:12;border-color:red'".rep lace(/([a-z]+-?[a-z]+):([a-z0-9]+)/gi,function(s){ found.push(s.sp lit(":"))})
alert(found)

The problem in exec method, not in regexp.
Note: I use .replace() method but there is no replacement in code
above, only matching

Nov 3 '06 #6
runsun pan wrote:
I am wondering why I couldn't get what I want in the following 3
cases of re:
You're expecting things to work in ways that shouldn't happen.
var p=/([a-zA-Z]+-?[a-zA-Z]+):([a-zA-Z0-9]+)/g
p.exec("style=' font-size:12'")
--[font-size:12,font-size,12] // expected
The RegExp.prototyp e.exec method attempts to find the first pattern
match in its argument. Here, it discovers "font-size:12", so the method
stops and returns an array containing details of the match. As the
global flag is set for the regular expression object, the lastIndex
property of the regular expression is modified to point just beyond the
end of the match: to the last apostrophe, in this case.
(B)

p.exec("style=' font-size:12;border-color:red'")
--[border-color:red,borde r-color,red]
That isn't actually what you receive. You've executed this straight
after the previous test, therefore the change in the lastIndex property
described above is significant. If this call was made separately, the
same result as above would be returned. That is, information about the
font-size declaration.
// expected: [font-size:12,font-size,12,
border-color:red,borde r-color,red]
That will never happen. The exec method only cares about the first (or
more generally, the next) match. Once it finds it (or the string has
been exhaustively searched with no match) the method will return.

As in a recent thread, "regexp test function behavior"[1], you too have
misunderstood how both the global flag and the exec method works. The
point of the former is, as I wrote in that thread, to allow repeated
processing by tracking where the last match ended. A follow-up call can
then resume from that point.

Some regular expression-related methods, most notably the
String.prototyp e.match and replace methods use the global flag to infer
that this repetitive process should be performed by the method itself,
using the behaviour of the RegExp.prototyp e.exec method to accomplish
that goal. If you want that same behaviour, you have to implement it
yourself:

String.prototyp e.detailedMatch = function(regExp ) {
var lastMatch = regExp.lastInde x = 0,
result = [],
match;

while ((match = regExp.exec(thi s))) {
result[result.length] = match;
if (regExp.lastInd ex == lastMatch) ++regExp.lastIn dex;
lastMatch = regExp.lastInde x;
}
return result;
};

Calling that:

'style="font-size:12;border-color:red"'.det ailedMatch(
/([a-zA-Z]+-?[a-zA-Z]+):([a-zA-Z0-9]+)/g);

would result in:

[[font-size:12,font-size,12],
[border-color:red,borde r-color,red]]

which is easy to observe with:

Array.prototype .toString = function() {
return '[' + this.join() + ']';
};

Compare that result with just the String.prototyp e.match method:

[font-size:12,border-color:red]

Mike
[1] regexp test function behavior
"HopfZ" <ho******@gmail .com>
Sun, 29 Oct 2006 12:02:42 (UTC)
<11************ **********@h48g 2000cwc.googleg roups.com>

<http://groups.google.c o.uk/group/comp.lang.javas cript/msg/87d9d504bf05393 d>
Nov 3 '06 #7

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

Similar topics

7
4871
by: AnnMarie | last post by:
My JavaScript Form Validation doesn't work at all in Netscape, but it works fine in IE. I made some of the suggested changes which enabled it to work in IE. I couldn't make all the changes because then it didn't work in IE. How can I enable this javascipt form validation to work in Netscape? When I use netscape, none of the alert boxes...
5
2736
by: Gary Mayor | last post by:
Hi, If I have the ' character within the javascript:pick command it doesn't work. Is there some sort of way of escaping these characters like in server side languages. function pick(symbol) { if (window.opener && !window.opener.closed) window.opener.document.create.text1.value = symbol; window.close(); }
3
25304
by: Matt | last post by:
I want to know if readOnly attribute doesn't work for drop down list? If I try disabled attribute, it works fine for drop down list. When I try text box, it works fine for both disabled and readOnly attribute. For example, #1 will work, but #2 doesn't work 1) <SELECT name="streetDirection" class="FormInput" DISABLED> In JavaScript, I have...
6
13262
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileID.ToString() + ".pdf");
4
3395
by: bbp | last post by:
Hello, In an ASPX page I have a "Quit" button which make a simple redirect in code-behind. This button doesn't work no more since (I think) I moved from the framework 1.0 to 1.1 and it doesn't work only on IE! My ASPX page works in FireFox. If the button doesn't work it's because of validators (and it worked with the same code in v1.0).
3
1808
by: Dave Moore | last post by:
Hi All, Ok, here's my problem. I want to open a file and process its contents. However, because it is possible that the file may not exist, I also want to check whether the file() function is successful before attempting to process any returned data from the file() function. The php file() manual page suggests that it returns FALSE if it is...
10
1798
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET framework 2.0, and he had 1.0 and it didn't work; then he installed 2.0 and it still didn't work; so he tried with 2.1 and it didn't work, then 3.0...
0
836
by: cnb | last post by:
Python-mode worked right out out of the box in Emacs and then I added Python to my path so now the interpreter works as well. However when I try to load a file(that is not located in site- packages) it doesn't work. ImportError: No module named parsing It is fairly impractical to work with if that doesn't work and cut and pasting...
1
2674
by: windscar | last post by:
Hello Everybody! I am a beginner that try to learn C++ programming. I work in Linux Ubuntu Karmic Koala (9.10) environment. I got a problem with getchar(). The function works well in a simple program. But when I use it in while (cond.) and switch (var.) , the function doesn't work, it doesn't even ask me to input character (including "\n")....
0
7512
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7438
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7951
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.