tags:

views:

58

answers:

2

I started off naming my jQuery objects like $this.

Then, I decided I didn't want to do it anymore.

I just borrowed a large chunk of code from an old project. It is from the $ prefix days, and it is irking me!

What regex can I use in my IDE to get rid of the $ prefixes on my variables?

+1  A: 

Try this...

/\$(?!\()/

...and obviously replace with an empty string ''.

This has some pretty serious limitations, but if you don't have a abundance of strings which may contain this pattern, it is good enough.

alex
If you know the answer, why are u posting on StackoverFlow????
wiz kid
@wiz kid From the [FAQ](http://stackoverflow.com/faq) `It's also perfectly fine to ask and answer your own question, as long as you pretend you're on Jeopardy: phrase it in the form of a question.`. It benefits other users with the same problem. It also gives other SO users a chance to give an alternative answer, or improve on my code.
alex
@wiz Because it's a perfectly legit thing to do. Other people may have the same question and may find this helpful.
deceze
@Deceze: I totally agree with you, but this guy answered the question within a minute, after posting this question....
wiz kid
@wiz kid Read the last sentence from my comment. I also can not accept my answer for 48 hours - if I *do* decide to. As for posting it within a minute - that was merely for my convenience. If you have an answer, post it.
alex
@wiz ...so? It's not like he's gained any reputation from it (yet), he just contributed to the SO knowledge pool. If you have a better, longer, more detailled answer, feel free to post it.
deceze
The real problem is that the regex doesn't seem to work.. Consider this piece of javascript:var text = 'one = 1; $two = 2; $three=3;';var regex = /\$(?!\()/;text = text.replace(regex, '');alert(text);
cambraca
@cambraca Use the global flag `g` and you are good to go. I didn't intend to run the regex in JavaScript, just once off in my IDE.
alex
Ok, how about this (it breaks the "one" variable): var text = 'one = "on $e"; $two = 2; $three=3;'; var regex = /\$(?!\()/g; text = text.replace(regex, ''); alert(text);
cambraca
@cambraca It sure does, but see the edit :)
alex
ha, nice :-) this is actually very difficult to do 100% correctly, so I'd advise just to use that regex and pray to [FSM](http://www.venganza.org/) nothing breaks
cambraca
@cambraca Definitely. My function was only about 40 lines and only had 1 string, so I knew this would be good enough. Thanks for the entertaining link.
alex
+1  A: 

To do it right, you will have to parse the javascript and replace only identifiers and not literals. Of course, it may not be worth the effort if your codebase is pretty small.

There are a few javascript parsers out there - ANTLR, google closure compiler and JS lint may have an implementation.

Chetan Sastry
+1 You are definitely right - that is the right way to do it. I just went with the *quick and nasty but suits my once off requirement fine* way :)
alex