How to convert “camelCase” to “Camel Case”?

后端 未结 11 940
北荒
北荒 2020-11-27 10:25

I’ve been trying to get a JavaScript regex command to turn something like \"thisString\" into \"This String\" but the closest I’ve gotten is replac

11条回答
  •  一向
    一向 (楼主)
    2020-11-27 11:02

    Try this solution here -

    var value = "myCamelCaseText";
    var newStr = '';
    for (var i = 0; i < value.length; i++) {
      if (value.charAt(i) === value.charAt(i).toUpperCase()) {
        newStr = newStr + ' ' + value.charAt(i)
      } else {
        (i == 0) ? (newStr += value.charAt(i).toUpperCase()) : (newStr += value.charAt(i));
      }
    }
    return newStr;
    

提交回复
热议问题