JavaScript Snippet – Undo Camel Case

Probably due to it being so late, I was looking for code to uncamelize (undo camel-casing) any string. I came across what claimed to be a solution in PHP but unfortunately did nothing but lowercased my string. Therefore I decided to write my own solution:

function uncamelize(s) {
  return s.replace(/[A-Z]/g, '_$&').toLowerCase();

Believe it or not, the solution is that simple. Here is an example of using it:

An interesting thing to note about this uncamelize implementation is that it uses the $& pattern to reuse the substring matched by the regular expression. Even though this substring is commonly used it is documented as shown here.

Leave a Reply

Your email address will not be published. Required fields are marked *

5 − = two

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">