Regular expressions sub/gsub for JQuery

Mar 9 2010 by Andrew Brown

I spent 1.5 hours searching the Internet and the JQuery docs to figure out how to perform sub or gsub on a string with a regular expression. I had to use a crazy mix of keywords to find the answer. Hopefully this blog post gets index on Google so the next person looking to do regular expressions in JQuery doesn’t have to waste so much time looking for the sub or gsub equivalent because the JQuery people can’t stick to programming conventions that makes sense and are already known by most programmers. This is a case of unnecessary naming cleverness. Give me back my time JQuery, you time vampire.

Oh, let us not forget the solution:

"This is a test".replace(/test/,'an explosion')

1 Comment to “Regular expressions sub/gsub for JQuery”

Darren

Wow, thanks… so surprising how you get on a track to find something you don’t stand back and say wait, I use a similar function all the time called replace that does the same thing. Ruby is killing the js side of the brain. You saved me 1.5 hrs of Google-ing.