Regex Fun! (and help requested)

I love Regex. I love the pure logical exercise of pattern-matching and escaping. I like than I can do things like close all empty tags that aren’t properly closed. I love that I quickly replace all &s with &. I love that the SmartyPants plugin for Movable Type exists, and makes the output here (and now, in a slightly less beatiful way, the Pencilcase) look so beaitful for everyone who reads it (ok, probably only typographers or designers who’d actually notice what type of apostrophes and quotes you’re using).

But I’ve come across a problem I can’t currently fix. And it has to do with unquoted attributes in links. I want, and am currently unable to create in a bug-free way, a regex that will identify an attribute within a tag (and it’s important that it only looks at this within a tag), and, if the attribute’s value does not have double-quotes around it, add double quotes. Currently, I have a regex that will put double-quotes around all attributes, regardless of whether or not there are quotes already. Which, obviously, isn’t quite right. It’s also ignorant as to whether or not this is taking place within a tag (although that’s an easy, if not elegant fix, of simply looping through all the tags, and then doing this regex. In an ideal world, I’d find a Regex that does all of this in one step). I suspect there’s a simple error in my logic that I haven’t quite accounted for, as clearly, I’m very close to at least having the basics licked here.

So, dear & darling readers, if any of you happen to have a regex that does exactly what I want (FYI – I’m ok with this Regex failing if the unquoted attribute value contains spaces – because then who the hell would know where it ends!? But it does need to be able to handle punctuation just fine, for links and path-strings, etc), I would be most obliged. I’m not quite sure what I have to offer you in return for your magic, but at the very least, I’ll send you warm fuzzy thoughts for days on end.

2 Replies to “Regex Fun! (and help requested)”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: