Skip to content

Instantly share code, notes, and snippets.

@cmjaimet
Created February 19, 2019 16:24
Show Gist options
  • Save cmjaimet/38d7aca612a63eaca812888c0161ddf6 to your computer and use it in GitHub Desktop.
Save cmjaimet/38d7aca612a63eaca812888c0161ddf6 to your computer and use it in GitHub Desktop.
zebqNv
<h2>Convert camel case to snake case</h2>
<p>Enter a camel case string.</p>
<form>
<input type="text" id="camel" value="camelCaseWord" />
<button id="butn" type="button">Go!</button>
</form>
<div id="output"></div>
document.getElementById('butn').addEventListener( 'click', function() {
let camel = document.getElementById( 'camel' ).value.trim();
let output = camel.replace( /\s/g, '' );
output = output.replace( /.[A-Z]/g, function( x ) {
return x[0]+"_"+x[1].toLowerCase();
} );
output = output.toLowerCase();
document.getElementById( 'output' ).innerHTML = output;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment