Comments on: `.match()` trick https://j11y.io/javascript/match-trick/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Rudie https://j11y.io/javascript/match-trick/#comment-2128 Wed, 19 Oct 2011 07:24:58 +0000 https://j11y.io/?p=1801#comment-2128 @MGoulart

Actually this is double redundant/bad:

var e = e || event;

because 1) e alreay exists in the scope and 2) you might be assigning e to e like this.

Better (and perfect and the only right syntax) would be:

e || (e=event);

Stil bad would be:

e = e || event; // at least no var

@DataZombies

You forgot to remove the , 10. Now the comma operator comes into play and it always uses 10 and never the regex result. Comma operator? Si.

Great post

]]>
By: John Weis https://j11y.io/javascript/match-trick/#comment-2127 Wed, 19 Oct 2011 07:09:02 +0000 https://j11y.io/?p=1801#comment-2127 Solid. Thanks so much James! I love this language more every day…

]]>
By: Sam https://j11y.io/javascript/match-trick/#comment-2126 Thu, 29 Sep 2011 04:31:41 +0000 https://j11y.io/?p=1801#comment-2126 Nice post mate, enjoyed it!

]]>
By: DataZombies https://j11y.io/javascript/match-trick/#comment-2125 Sat, 20 Aug 2011 13:55:32 +0000 https://j11y.io/?p=1801#comment-2125 Or, Craig, you can do my favorite tick…

~~(( thatString.match(/result_count=(d+)/) || [,5] )[1], 10)
]]>
By: craig https://j11y.io/javascript/match-trick/#comment-2124 Mon, 18 Jul 2011 13:37:11 +0000 https://j11y.io/?p=1801#comment-2124 If you wanted to make sure you were dealing with a number you can wrap it even more!

parseInt(( thatString.match(/result_count=(d+)/) || [,5] )[1], 10)
]]>
By: Adam Eivy https://j11y.io/javascript/match-trick/#comment-2123 Tue, 12 Jul 2011 19:14:51 +0000 https://j11y.io/?p=1801#comment-2123 The way javascript handles logical OR in assignment is one of my favorite things about the language. It allows you to do so many things more elegantly:

var conf = obj.conf || {
   default:'conf'
};

etc… good times.

]]>
By: MGoulart https://j11y.io/javascript/match-trick/#comment-2122 Tue, 12 Jul 2011 15:03:38 +0000 https://j11y.io/?p=1801#comment-2122 I used this pretty often with event global object for Internet Explorer.

function myEventHandler (e) {
    var e = e || event;
    // proceed to use e
}
]]>
By: Joss https://j11y.io/javascript/match-trick/#comment-2121 Mon, 11 Jul 2011 02:50:04 +0000 https://j11y.io/?p=1801#comment-2121 I love these little language gotchas, especially when they’re useful like this!

]]>
By: Den O https://j11y.io/javascript/match-trick/#comment-2120 Sun, 10 Jul 2011 18:26:43 +0000 https://j11y.io/?p=1801#comment-2120 Genius, as always, James!

]]>
By: Andy E https://j11y.io/javascript/match-trick/#comment-2119 Sun, 10 Jul 2011 18:24:21 +0000 https://j11y.io/?p=1801#comment-2119 I used to use this technique all the time when accessing fields from the eBay web service. For instance, if I was pulling the prices from an item I’d have something like this:

var dummy = {text:""},
    bin = (xmlDoc.selectSingleNode("/root/item/BuyItNowPrice") || dummy).text,
    offer = (xmlDoc.selectSingleNode("/root/item/BestOfferPrice") || dummy).text;

instead of

var binNode = xmlDoc.selectSingleNode("/root/item/BuyItNowPrice"),
    bin = binNode ? binNode.text : "",
    offerNode = xmlDoc.selectSingleNode("/root/item/BestOfferPrice"),
    offer = offerNode ? offerNode.text : "";

Sometimes I’d have result sets where there could be about 5 optional fields, so this simplified things without having to write a separate function. It meant I could separate my logic from the parsing very easily. It was a trick I’d taught myself so I was really proud at the time, it’s one of the things that made me eager to refactor as much as possible 🙂 Of course, if there had been too many, I’d just write a function to handle it and let that function do the extra leg work to keep the code tight.

I do agree, though, that this is one of the sweeter sides to JS.

]]>