Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-08-2012, 08:45 PM   PM User | #16
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
It's not clear at all to me what your reason for using eval( ) is.

Is it simply so you can do it all in one line?

Not a good reason.

What's the difference between:
Code:
    replace(eval("/"+var1+"/gi"), anything)
and
Code:
    replace( (new RegExp( var1, "gi" ) ), anything )
???
Surely the second version is much more efficient, by avoiding the eval( ).

Even if eval( ) isn't evil, it's certainly less efficient than most anything else. (Especially in MSIE, unsurprisingly.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-08-2012, 11:02 PM   PM User | #17
rdspoons
New Coder

 
Join Date: Jun 2009
Posts: 81
Thanks: 0
Thanked 8 Times in 8 Posts
rdspoons is on a distinguished road
Old Pendant,

I've evaluated your comment and of course you're right. Thank you. I do fall back to eval when I believe it's required, but in the examples posted here I used eval because its a legal (if unwelcome) option that provided a solution. There is no doubt that replace((new RegExp(var1,"gi")),var2) is superior and more desirable, and as such, I stand corrected - in my evaluations.
rdspoons is offline   Reply With Quote
Old 12-08-2012, 11:11 PM   PM User | #18
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I'd be really interested to see a situation where eval() is *REQUIRED*.

Other than parsing JSON that is returned by an AJAX call or something very similar.

I have yet to find a case where it is required, except when processing external JS data input.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-09-2012, 01:38 PM   PM User | #19
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
I forgott the / which is also to escape with javascript !

Code:
String.prototype.toRegExp=function(){return this.replace(/\\/g,'\\\\').replace(/(\[|\^|\$|\.|\||\?|\*|\+|\(|\)|\/)/g,'\\$1')}

//Example of use with all the «meta»characters
var something="[\\^$.|?*+()/"; // the slashe is to escape
	
alert(("foo"+something+"bar").replace(new RegExp(something.toRegExp(),'g'),''));// =>'foobar'
007julien is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:09 PM.


Advertisement
Log in to turn off these ads.