Flagrant Badassery

A JavaScript and regular expression centric blog

Archive for June, 2008

Code Challenge: Change Dispenser

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

Multiple String Replacement Sugar

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 , '&amp;' ). replace( /"([^"]*)"/g , '“$1”' ). replace( /</g , '&lt;' ). replace( />/g , '&gt;' […]

Read More