Recent Widgets


Register for DashboardWidgets

Recent Forums Posts

Partners


iCompositions

MacDesktops.net

RSS Showcase
RSS Comments
RSS Forums

This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. Posted in: Widget Development

Javascript Regular Expression help

Author Message
sean



Joined: 22 Apr 2005
Posts: 67
Location: Duluth, MN

Posted: Sun Apr 24, 2005 - 7:23 pm    Post subject: Javascript Regular Expression help Reply with quote

Hey everyone, I'm having a bit of trouble writing a regexp to change the Edit links in my wikipedia widget to call Safari.

Basically, I need to go from this:

Code:
[<a href=
"/w/index.php?title=Scolecomorphidae&amp;action=edit&amp;section=1"
title="Scolecomorphidae">edit</a>]


(newline included)

To this:

Code:
[<a href="widget.openURL('http://en.wikipedia.org/w/index.php?title=Scolecomorphidae&amp;action=edit&amp;section=1')
title="Scolecomorphidae">edit</a>]


So, if you're having a slow day at work, and you really love regular expressions, feel free to lend a hand. Wink
View user's profile Send private message Send e-mail Visit poster's website Widgets
Chris



Joined: 27 Jan 2005
Posts: 344
Location: Durham, UK

Posted: Mon Apr 25, 2005 - 2:07 am    Post subject: Reply with quote

I've only just got up, so apologies if this is a naff solution.

This first way assumes you're using DOM methods to make the anchor, and it'll use a regex:

Code:

var info = str.match(/<a href="([^>]*)" title="([^>]*)"/);

var edit = document.createElement("a");
edit.setAttribute("href", "widget.openURL('" + info[1] + "')");
edit.setAttribute("title", info[2]);
edit.appendChild(document.createTextNode("Edit"));


or the innerHTML route:

Code:

var a_str = "<a href=\"" + info[1] + "\" title=\"" + info[2] + "\">Edit</a>";


Of course, you could also just use standard JS string methods to alter the original string and use that, if you use innerHTML.
View user's profile Send private message Send e-mail Visit poster's website AIM Address Widgets
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.

 
Powered by phpBB © 2001, 2002 phpBB Group