Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check if a string is Camel Case using Javascript
String.prototype.toCamelCase = function(cap1st) {
'use strict';
return ((cap1st ? '-' : '') + this).replace(/-+([^-])/g, function(a, b) {
return b.toUpperCase();
});
};
function isCamelCase(str){
'use strict';
var strArr = str.split('');
var string = '';
for(var i in strArr){
if(strArr[i].toUpperCase() === strArr[i]){
string += '-'+strArr[i].toLowerCase();
}else{
string += strArr[i];
}
}
if(string.toCamelCase(true) === str){
return true;
}else{
return false;
}
}
console.log(isCamelCase('CamelCase'));
@evonsdesigns

This comment has been minimized.

Copy link

commented Jul 30, 2014

This interprets numeric strings as CamelCase

@reyronald

This comment has been minimized.

Copy link

commented May 22, 2018

I think this suffices for most cases:

function isCamelCase(str) {
  return /[A-Z]/.test(str)
}
@leandroruel

This comment has been minimized.

Copy link

commented Jun 4, 2018

wow, above comment fix all with a simple solution

@mdusafp

This comment has been minimized.

Copy link

commented Jun 18, 2019

@reyronald it fails on 'snake_Case'

> /[A-Z]/.test('snake_Case')
true
@reyronald

This comment has been minimized.

Copy link

commented Jun 18, 2019

Then just add && !str.includes('_') to the returning condition @mdusafp.

@mdusafp

This comment has been minimized.

Copy link

commented Jun 18, 2019

so you should do it for all type of cases)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.