javascript - Regular Expression: Convert a numeric string from camelCase to regular -


i need convert alphanumeric string, written in camelcase regular string. input can of following strings:

var strs = {     input: [         "thisstringisgood",         "somethinglikethis",         "a123capshere345andherebutnot678here90end",         "123capshere345andherebutnot678here90e",         "abcacryonym",         "xmlhttprequest",     "thisdevicehasa3printingservice"     ],     expectedresult: [         "this string good",         "something this",         "a123 caps here345 , here not678 here90 end",         "123 caps here345 , here not678 here90 e",         "abc acryoynm",         "xml http request",     "this device has a3 printing service"     ] }; 

after converting string expected format i'm doing this:

function capsplit(str) {         return str.replace(/(^[a-z]+)|[0-9]+|[a-z][a-z]+|[a-z]+(?=[a-z][a-z]|[0-9])/g, function (match, first) {             if (first) match = match[0].touppercase() + match.substr(1);             return match + ' ';         })     } 

but problem spaces added numbers well. need convert string "thisdevicehasa3service" converted "this device has a3 printing service" function capsplit(str) gives me this device has 3 printing service don't know i'm missing. here fiddle

this solution found:

function capsplit(str) {   return str.replace(/([a-z][a-z])|([a-z][a-z])|(\d[a-za-z])/g, function (g, m1, m2, m3) {     return m1 ? " " + m1 : (m2 ? m2[0] + " " + m2[1] : m3[0] + " " + m3[1]);   }).replace(/^[a-z]/g, function (g) {     return g.touppercase();   }); } 

the main regexp matches every situation space needs inserted , function inserts space it's required


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -