tips and tricks for movable type
escaping single quotes with regex
October 13, 2002
There's one rule I've always followed when giving titles to my entries in my journal and blog - I never use titles with single quotes in them.
The reason? I use mouseovers on my blog calendar, and in my journal navigation menu, which show the entry title in the status bar. A title with a single quote in it causes a Javascript error - because a quote is one of those characters that must be escaped with a backslash so it won't be interpreted as part of the code.
So I turned to Brad Choate's Regex plugin to solve the problem.
This is the Regex statement:
See? Even writing the Regex statement is tricky! The first \' tells it to look for just the ' (gotta escape it in Regex too!). The next pair of slashes \\ tells it to replace the quote with a \, and then a ' (with the third escape: \').
So ' becomes \' - which is what I want to make the mouseovers work.
Did you get all that? It sure made my head spin for a moment.
Oh, the final thing is to add it to my entry title tag inside my mouseover statement:
Now, a title like I'm a geeky girlie! becomes I\'m a geeky girlie! - exactly what I need to avoid that nasty Javascript error.
Yay! Now I don't have to think so hard to avoid using single quotes in my entry titles (you didn't realize how complicated my life actually was, did you?)
Update 11.09.02: Of course, if I'd paid better attention to the MT manual, I would have learned this issue was more simply resolved:
DOH!!
Comments
Very cool! I have a hard time keeping those regex statements from making me go crazy!!! So much escaping and escaping escapes LOL!
self.status? Where does that show up then?
My goodness, you're speedy! =)
self.status makes it show up in the status bar at the bottom of the browser window. Then I use onMouseOut to make the title go away when the mouse moves elsewhere:
onMouseOut="self.status='';return true"
(That's two single quotes after self.status=.)
What I've yet to figure out is the difference between return true and return false, LOL!
Oh, I guess I've always seen window.status - but I guess they do the same thing :) I don't know either about return true vs. return false. :)
And I was sitting here when the notification came in! Hence the speedyness!!
How do you ge the Blog Status Script and how do i get a "subscribe to comments" for AnonBlog ?
Blog Status: MTBlogEntryCount and MTBlogCommentCount and LGF referrers
Scriptygoddess Subscribe to Comments script
well if you put return false it won't execute. That's the difference..
BTW I saw that you replied on my topic at the forums..that's how I got here. I just wanted to write my method of upgrading to 2.5.
I just downloaded the Full version download. ANd installed it at new location and then copied my old db folder into the new location and it works like a charm.
I just couldn't get the old location to upgrade. I kept getting a error.
Now keep in mind the URL to my blog is still the same the only thing that is different is my login URL.