Recent Widgets


Register for DashboardWidgets

Recent Forums Posts

Partners


iCompositions

MacDesktops.net

RSS Showcase
RSS Comments
RSS Forums

Post new topic   Reply to topic Posted in: Widget Development

RegExp.exec()

Author Message
sixohseven



Joined: 02 Sep 2005
Posts: 6

Posted: Wed Aug 30, 2006 - 10:54 pm    Post subject: RegExp.exec() Reply with quote

Ok,

I'm having a lot of trouble with JavaScript regular expressions. According to the documentation I've read if you use exec() it returns an array of all matching results in the given string.

I'm using the following:
Code:
var resultArray = re.exec(resultString);


I'm assuming that this returns all of the occurrences that match the regular expression, but I'm only ever getting one result... the first. I don't even care what the results are, I just need to know how many occurrences there are. I was hoping to just check the length of the array after exec returned.

Any suggestions on how to make exec work for me, or other suggestions on how to quickly and easily find the number of occurrences of a few different regexes would be helpful. I'd prefer not to have to loop over a bunch of find statements.
View user's profile Send private message Widgets
seb2



Joined: 04 May 2005
Posts: 41

Posted: Thu Aug 31, 2006 - 3:28 am    Post subject: Reply with quote

Works fine for me.

What does your regular expression look like? Does it have the /g switch at the end?
View user's profile Send private message Send e-mail Widgets
sixohseven



Joined: 02 Sep 2005
Posts: 6

Posted: Thu Aug 31, 2006 - 6:34 am    Post subject: Reply with quote

Well, my regex is very, very simple. This is what it looks like:
Code:
var reIPAddress = /[\d]*\.[\d]*\.[\d]*\.[\d]*/g;
var resultArray = reIPAddress.exec(resultString);
alert(resultArray.length);

In the interest of full disclosure I added the 'g' after your posting. I'm still a relative regex novice. I re-ran my widget and got the same response. resultArray.length is still 1 and it contains a single ip address. However, there are multiple ip addresses in resultString, so it would seem to me that I should get more than one result. TextWrangler finds me more ip addresses when I do find using grep and find again. TextWrangler doesn't support /g.
View user's profile Send private message Widgets
seb2



Joined: 04 May 2005
Posts: 41

Posted: Thu Aug 31, 2006 - 10:52 am    Post subject: Reply with quote

OK, I'm no RegEx expert, either, but I doubt you'll get this to work if the number of IPs is unknown. Are they separated from another by a known string? You could simply use split() then.

What I know is that square brackets are being used for character classes defined by you, \d is a, well, "built-in character class" that doesn't require square brackets. I'm surprised this works at all.
View user's profile Send private message Send e-mail Widgets
sixohseven



Joined: 02 Sep 2005
Posts: 6

Posted: Thu Aug 31, 2006 - 2:49 pm    Post subject: Reply with quote

\d may not "require" square brackets, and you may be headed in the right direction that adding them may affect performance, but it's technically still a good regex.

I'm afraid I'm going to have to do what I was hoping to avoid... split using "\n" and loop over that array because I know there's no more than one per line, so if I check each line I can get a good count.

...poo.
View user's profile Send private message Widgets
Post new topic   Reply to topic

 
Powered by phpBB © 2001, 2002 phpBB Group