javascript - Using RegEx to separate camelCase - how do i include special characters? -
this syntax using:
.split(' ') .join('+') .replace(/([a-z])/g, ' $1') .replace(/^./, function(str) { return str.touppercase(); }) .replace(/\++/g, '') .replace(/asdf/g, ';') .replace(';', '') .replace(' ', '') .split(';');
this code helps me turn 'camelcase' 'camel case'. try variety of other things, replacing excess spaces 1 space through '+'s. replace 'asdf' ; , cut of first ; , first space.
problem: @ same time though, breaks german special characters, namely ÄÖÜ. turning them question marks in output.
i believe generates output this: input: ... oberbayern:kreis eichstättasdf ... output: ... oberbayern: kreis eichst�tt; asdf ...
please me.
you can try this:
logic
- check small character followed caps character.
- replace adding space it.
- now make 1st character uppercase
note: pointed out t.j. crowder, better use string.tolocaleuppercase, if available.
function seperatecamelcasewords(a) { var regex = /([a-z])(?=[a-z])/g; = a.replace(regex, "$1 "); = upper(a.charat(0)) + a.substring(1) console.log(a) return a; } function upper(str){ return str.tolocaleuppercase ? str.tolocaleuppercase() : str.touppercase(); } var = "thisisteststring"; var b = "kreiseichstätt" seperatecamelcasewords(a); seperatecamelcasewords(b);
you can use following logic match character except space followed capital character.
function seperatecamelcasewords(a) { var regex = /([^ ])(?=[a-z])/g; = a.replace(regex, "$1 "); = upper(a.charat(0)) + a.substring(1) console.log(a) return a; } function upper(str){ return str.tolocaleuppercase ? str.tolocaleuppercase() : str.touppercase(); } seperatecamelcasewords("thisisateststring"); seperatecamelcasewords("kreiseichstätt"); seperatecamelcasewords("kreisäeichstätt")
Comments
Post a Comment