javascript regex camelcase -


i'm getting location.path url , display url title, how can make camelcase if have title of "banana , apple" don't want "and" "and" instead return "banana , apple"

i found javascript code , returning title "banana , apple".

function camelize(str) {         return str.replace(/(?:^\w|[a-z]|\b\w|\s+)/g, function(match, index) {             if (+match === 0) return " "; // or if (/\s+/.test(match)) white spaces             return index == 0 ? match.tolowercase() : match.touppercase();         });     } 

and i'm replacing (/) , (-) space

this.title = camelize(lastpartofurl.replace(/\/|\-/g, ' ')); 

or

var cleanurl = lastpartofurl.replace(/\/-/g, ' '); this.title = camelize(cleanurl.replace(/-/g, ' ')); 

there shorter solution based on single regex.

function camelcase(txt){    var lower='and,or,a,in,on,'; //stop-words    return txt //.split(/\/-/).join(' ')            .tolowercase().replace(/(\b.)(\s*\b)(\s|$)/g,                       function(m/*full match*/,                                n/*(\b.)*/,                                p/*(\s*\b)*/,                                q/*(\s|$)*/){                         //console.log(n,p);                         return (lower.indexof(n+p+',')===-1?                           n.touppercase() : n) + p + q;}); } 

Comments