Flagrant Badassery

A JavaScript and regular expression centric blog

RSS Feed for JavaScriptJavaScript

Unicode Plugin for XRegExp

I've released a simple plugin for XRegExp (my JavaScript regex library) that adds support for Unicode properties and blocks to JavaScript regular expressions. It uses the Unicode 5.1 character database, which is the very latest version. The Unicode plugin enables the following Unicode properties/categories in any XRegExp: \p{L} — Letter \p{M} — Mark \p{N} — Number \p{P} — Punctuation \p{S} — [...]

Read More

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 = [], [...]

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 [...]

Read More

Remove Nested Patterns with One Line of JavaScript

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 <…> (where angled brackets are [...]

Read More

Test Your XRegExps with JRX

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 for [...]

Read More