Insert space before capital letters

前端 未结 8 1178
误落风尘
误落风尘 2020-12-02 07:01

I have a string \"MySites\". I want to place a space between My and Sites.

How can I do this in jQuery or JavaScript?

相关标签:
8条回答
  • 2020-12-02 07:31

    This should insert a space between each capital letter that was not preceded by a capital letter.

    var myString = "MySites"
    var newString = "";
    var wasUpper = false;
    for (var i = 0; i < myString.length; i++)
    {
        if (!wasUpper && myString[i] == myString.toUpperCase()[i])
        {
            newString = newString + " ";
            wasUpper = true;
        }
        else
        {
            wasUpper = false;
        }
        newString = newString + myString[i];
    }
    

    newString will have the value you want. Also, if you want to shorten your code using regex, you can use the following code from Javascript camelCase to Regular Form

    "thisStringIsGood"
        // insert a space before all caps
        .replace(/([A-Z])/g, ' $1')
        // uppercase the first character
        .replace(/^./, function(str){ return str.toUpperCase(); })
    
    0 讨论(0)
  • 2020-12-02 07:37

    In a single regex replace (without trims or joins) that allows any character not just letters [a-z] or [A-Z].

    const str = "MySites";
    str.replace(/(?<!^)([A-Z])/, " $1"); // -> "My Sites"
    

    You can check more about the look behind (?<!^) here.

    0 讨论(0)
  • 2020-12-02 07:41

    You can use String#split() and a look-ahead for the capitalized alphabet ([A-Z]) and then Array#join() the array with a space:

    let stringCamelCase = "MySites";
    
    let string = stringCamelCase.split(/(?=[A-Z])/).join(" ");
    
    console.log(string)

    Or, as a String Object function:

    String.prototype.cC2SC = function() {
      return this.split(/(?=[A-Z])/).join(" ");
    }
    
    console.log("MyCamelCase".cC2SC());

    0 讨论(0)
  • 2020-12-02 07:51

    This will find each occurrence of a lower case character followed by an upper case character, and insert a space between them:

    s = s.replace(/([a-z])([A-Z])/g, '$1 $2');
    

    For special cases when 2 consecutive capital letters occur (Eg: ThisIsATest) add additional code below:

     s = s.replace(/([A-Z])([A-Z])/g, '$1 $2');
    
    0 讨论(0)
  • 2020-12-02 07:51

    Here is what i ended up using to convert a string to a title case, based on a few of the answers here:

    str = str
      .replace(/(_|-)/g, ' ')
      .trim()
      .replace(/\w\S*/g, function(str) {
        return str.charAt(0).toUpperCase() + str.substr(1)
      })   
      .replace(/([a-z])([A-Z])/g, '$1 $2')
      .replace(/([A-Z])([A-Z][a-z])/g, '$1 $2')   
    

    Here is a JSFiddle where you can test your string to see if this meets your needs: https://jsfiddle.net/thomastasa/5236dv8t/85/


    Examples:

    • "yourStringHere" -> "Your String Here"
    • "AnotherStringHere" -> "Another String Here"
    • "someones_string" -> "Someones String"
    • "Another-String-Here" -> "Another String Here"
    • "myAWESOMEString" -> "My AWESOME String"
    0 讨论(0)
  • 2020-12-02 07:54

    regex to find lower case - upper case boundary then insert a space

    <div id='x'>ThisIsMySites</div>
    $('#x').text( $('#x').text().replace(/([a-z])([A-Z])/g, "$1 $2") );
    

    http://jsfiddle.net/uXy64/

    0 讨论(0)
提交回复
热议问题