Convert camelCaseText to Sentence Case Text

后端 未结 20 2052
闹比i
闹比i 2020-11-28 03:44

How can I convert a string either like \'helloThere\' or \'HelloThere\' to \'Hello There\' in JavaScript?

相关标签:
20条回答
  • 2020-11-28 04:24

    Below is link which demonstrates camel case string to sentence string using regex.

    Input

    myCamelCaseSTRINGToSPLITDemo

    Output

    my Camel Case STRING To SPLIT Demo


    This is regex for conversion of camel case to sentence text

    (?=[A-Z][a-z])|([A-Z]+)([A-Z][a-rt-z][a-z]\*)
    

    with $1 $2 as subsitution.

    Click to view the conversion on regex

    0 讨论(0)
  • 2020-11-28 04:25
    var text = 'helloThereMister';
    var result = text.replace( /([A-Z])/g, " $1" );
    var finalResult = result.charAt(0).toUpperCase() + result.slice(1);
    console.log(finalResult);
    

    capitalize the first letter - as an example.

    Note the space in " $1".

    EDIT: added an example of capitalization of the first letter. Of course, in case the first letter is already capital - you would have a spare space to remove.

    0 讨论(0)
  • 2020-11-28 04:25

    Alternatively using lodash:

    lodash.startCase(str);
    

    Example:

    _.startCase('helloThere');
    // ➜ 'Hello There'
    

    Lodash is a fine library to give shortcut to many everyday js tasks.There are many other similar string manipulation functions such as camelCase, kebabCase etc.

    0 讨论(0)
  • 2020-11-28 04:25

    This works for me check this out

    CamelcaseToWord("MyName"); // returns My Name

        function CamelcaseToWord(string){
          return string.replace(/([A-Z]+)/g, " $1").replace(/([A-Z][a-z])/g, " $1");
        }
    
    0 讨论(0)
  • Ok, I'm a few years late to the game, but I had a similar question, and I wanted to make a one-replace solution for every possible input. I must give most of the credit to @ZenMaster in this thread and @Benjamin Udink ten Cate in this thread. Here's the code:

    var camelEdges = /([A-Z](?=[A-Z][a-z])|[^A-Z](?=[A-Z])|[a-zA-Z](?=[^a-zA-Z]))/g;
    var textArray = ["lowercase",
                     "Class",
                     "MyClass",
                     "HTML",
                     "PDFLoader",
                     "AString",
                     "SimpleXMLParser",
                     "GL11Version",
                     "99Bottles",
                     "May5",
                     "BFG9000"];
    var text;
    var resultArray = [];
    for (var i = 0; i < textArray.length; i++){
        text = textArray[i];
        text = text.replace(camelEdges,'$1 ');
        text = text.charAt(0).toUpperCase() + text.slice(1);
        resultArray.push(text);
    }
    

    It has three clauses, all using lookahead to prevent the regex engine from consuming too many characters:

    1. [A-Z](?=[A-Z][a-z]) looks for a capital letter that is followed by a capital then a lowercase. This is to end acronyms like USA.
    2. [^A-Z](?=[A-Z]) looks for a non-capital-letter followed by a capital letter. This ends words like myWord and symbols like 99Bottles.
    3. [a-zA-Z](?=[^a-zA-Z]) looks for a letter followed by a non-letter. This ends words before symbols like BFG9000.

    This question was at the top of my search results, so hopefully I can save others some time!

    0 讨论(0)
  • 2020-11-28 04:28

    You can use a function like this:

    function fixStr(str) {
        var out = str.replace(/^\s*/, "");  // strip leading spaces
        out = out.replace(/^[a-z]|[^\s][A-Z]/g, function(str, offset) {
            if (offset == 0) {
                return(str.toUpperCase());
            } else {
                return(str.substr(0,1) + " " + str.substr(1).toUpperCase());
            }
        });
        return(out);
    }
    
    "hello World" ==> "Hello World"
    "HelloWorld" ==> "Hello World"
    "FunInTheSun" ==? "Fun In The Sun"
    

    Code with a bunch of test strings here: http://jsfiddle.net/jfriend00/FWLuV/.

    Alternate version that keeps leading spaces here: http://jsfiddle.net/jfriend00/Uy2ac/.

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