Friday, June 20th, 2008 •
Related •
Filed Under
I recently encountered a brain teaser that asked to take an amount of change and return the equivalent in dollars and coins. Here's the five-minute solution I first came up with. function makeChange (money) { var i, num, output = [], coins = [ [100, "dollar", "dollars" ], [25, "quarter", "quarters"], [10, "dime", "dimes" ], [...]
Read More
Thursday, June 12th, 2008 •
Related •
Filed Under
How many times have you needed to run multiple replacement operations on the same string? It's not too bad, but can get a bit tedious if you write code like this a lot. str = str. replace( /&(?!#?\w+;)/g , '&' ). replace( /"([^"]*)"/g , '“$1”' ). replace( /</g , '<' ). replace( />/g , '>' [...]
Read More
Wednesday, May 28th, 2008 •
Related •
Filed Under
Here's a neat little trick I came up with for removing nested patterns from a string. var str = "abc<1<2<>3>4>def"; while (str != (str = str.replace(/<[^<>]*>/g, ""))); // str -> "abcdef" Notice that the regex in this one-liner doesn't try to deal with nested patterns at all. The while loop's condition replaces instances of <…> [...]
Read More
Friday, May 16th, 2008 •
Related •
Filed Under
Cüneyt Yılmaz's JRX is a cool JavaScript regex tester inspired by the RX tool of Komodo IDE. Cüneyt recently added my XRegExp library to his tester, so JRX is now a nice and easy way to test XRegExp's singleline and extended modes, as well as named capture and other XRegExp-provided syntax. Check it out! As [...]
Read More
Sunday, April 20th, 2008 •
Related •
Filed Under
Update: This version of XRegExp is outdated. See XRegExp.com for the latest, greatest version. If you haven't seen the prior versions, XRegExp is an MIT-licensed JavaScript library that provides an augmented, cross-browser implementation of regular expressions, including support for additional modifiers and syntax. Several convenience methods and a new, powerful recursive-construct parser that uses regex [...]
Read More