Limiting the contents of a string via RegEx

Often, you will need to prevent users from entering data that doesn’t conform to a specific pattern. For example, you may want to allow users to enter only numbers or only valid email addresses. To this end, I’ve written a little utility function that returns the “standardised” version of a string, according to the regex you supply.

	limitContent: function(allowedRegex) {
		return $splat(this.match(allowedRegex)).join('');

Basically, the function takes the result of evaluating the regular expression on the string, converts it into an array if it isn’t one, and then joins the array’s elements together with an empty string.


console.log("12345".limitContent(/.{4}/)); // Only allow four characters
console.log("".limitContent(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}/)); // Only allow email addresses

Internet censorship: Letter #1

The letter below is written to my local MP regarding Australia’s proposed internet censoring.

Dear Mr Keenan,

I am writing to you concerning the soon to be trialed internet filtering scheme. As a resident of the City of Stirling and a UWA-qualified computer scientist, I have reservations over the efficacy, utility, impact, and morality of this initiative.

Firstly, the internet encompasses more than just web pages and web sites. The so-called “deep web” is thought to be many times larger than the “surface web” (that which can be examined by Google, for example). Where the majority of websites use the HTTP protocol to transfer data, which is subject to filtering under the proposed system, the deep web makes use of a wide variety of alternative data interchange systems, including torrents, UseNet, and VPNs. None of these will be filtered under the scheme, yet it is here that much of the undesirable content is to be found. [1]

Prior to the change of federal government, a system was in place whereby anyone could obtain free filtering software from the Australian government. This software ran on the computers themselves, and thus placed the onus for preventing unsuitable material from arriving on those in charge of the computers. In other words, parents were responsible for the well-being and safety of their children whilst online — in my opinion, a far more desirable state of affairs. [2]

The federal government has been extolling the virtues of high-speed internet across the country. Whilst I applaud this initiative, I have to question the sense of improving internet speeds across the country, only to then drastically reduce them by the introduction of mandatory internet filtering. In tests, it has been shown that filtering can reduce access speed by 10ms, and, due to bottle-neck difficulties, much longer times. Surely this is nonsensical. [3]

My final, and perhaps most significant, issue with the proposed implementation is that the “blacklist” of blocked sites will be inaccessible to the public. Australia is a nation founded on the ideals of a free, democratic, and transparent government. To make this list unavailable suggests that the filtering may be politically or privately motivated, politicians’ assurances notwithstanding. [4]

I ask that you carefully consider the issues I have raised, and that you stand and speak against this system.

Yours faithfully,

Barry van Oudtshoorn


[Music] Lament

About a month ago, I was playing around on the piano, and I came up with a fragment of melody that I couldn’t get out of my head. I played with it, twisting it around, pushing it in different directions, seeing what I could do with it. Ultimately, I turned it into a full piece of music, and this is the result.

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

Like this track? Buy it at thesixtyone, and help feed an impoverished (well, not really) programmer.

You can also listen to one of the (early) improvisations I played on this theme by downloading the recording I made on my phone here.