CamelCase in ECMAScript

Example for versions SpiderMonkey 1.7

This example is meant to be executed from web-browser, same as quadratic equation. Input form should look like this:

<form name="CamelCase">
  <input type="text" required="required" name="txt">
  <input type="button" value="Convert to CamelCase" onClick="convert()">

The code itself could have been written in one line, but has been broken in several parts for better readability. First line gets the string to process; second line converts it to lower case and replaces all non-letter characters with spaces; third line capitalizes each word; and fourth line removes all spaces. JavaScript has a very strong support of regular expressions, so this is done easily.

function convert() {
  txt = document.CamelCase.txt.value;
  txt = txt.toLowerCase().replace(/[^a-z ]+/g, ' ');
  txt = txt.replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); });
  txt = txt.replace(/[^a-zA-Z]+/g, '');
  document.getElementById('output').innerHTML = txt;