Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: Change Case

  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    116
    Thanks
    19
    Thanked 1 Time in 1 Post

    Change Case

    Dear Experts

    Var str="this is a string"

    I want to know how to replace this with Uppercase while using regular expression.

    Please help

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Sounds like a homework question, so I'll just recommend using String.replace with an external function:

    https://developer.mozilla.org/en/Jav...as_a_parameter

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    What is wrong with .toUpperCase()?
    Javascript is case sensiive so Var will thrrow an error.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,610
    Thanks
    0
    Thanked 645 Times in 635 Posts
    It is always better to use a built in method where possible rather than a regular expression.

    var str="this is a string".toUpperCase();
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder
    Join Date
    Oct 2011
    Posts
    116
    Thanks
    19
    Thanked 1 Time in 1 Post
    You are right sir but i think nothing is wrong to learn new techniques.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,610
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by tqmd1 View Post
    You are right sir but i think nothing is wrong to learn new techniques.
    There is nothing wrong with learning new techniques but you should also learn when it is and isn't appropriate to use them.

    It isn't appropriate to use your own code to perform a task for which a built in call is provided as the built in call will use code compiled into the browser and so will run many times faster - also it will have been thoroughly tested and so will always work.

    Now a similar task that would be worth doing with a regular expression because there isn't anything built in to do it would be converting a string to title case (where you capitalise the first letter of each word andhave the rest in lowercase).

    One possible solution to doing this (which I wrote some time ago) would be:

    Code:
    String.prototype.toTitleCase = function() {
    var re = /\s/;
    var words = this.split(re);
    re = /(\S)(\S+)/;
    for (i = words.length - 1; i >= 0; i--) {
    re.exec(words[i]);
    words[i] = RegExp.$1.toUpperCase() + RegExp.$2.toLowerCase();
    }
    return words.join(' ');
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by felgall View Post

    Now a similar task that would be worth doing with a regular expression because there isn't anything built in to do it would be converting a string to title case (where you capitalise the first letter of each word andhave the rest in lowercase).

    One possible solution to doing this (which I wrote some time ago) would be:

    Code:
    String.prototype.toTitleCase = function() {
    var re = /\s/;
    var words = this.split(re);
    re = /(\S)(\S+)/;
    for (i = words.length - 1; i >= 0; i--) {
    re.exec(words[i]);
    words[i] = RegExp.$1.toUpperCase() + RegExp.$2.toLowerCase();
    }
    return words.join(' ');
    }
    If there's more than one space between words, that gives undesired results.
    Something like this maybe:
    Code:
    String.prototype.toTitleCase = function() 
    {
      var re = /(\b.)(\S*)/g;
        
      return this.replace( re, function( a, b, c ){ return b.toUpperCase() + c.toLowerCase(); } );   
    }
    
    alert("some random sentence".toTitleCase());

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Easist way to capitalise first letter:-


    Code:
    <script type = "text/javascript">
    var str = "jEAn-paul o'flaNAGan-macDONald is a               brilliant coder";
    str = str.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
    document.write(str); 
    </script>
    Last edited by Philip M; 01-24-2012 at 07:45 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,610
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Philip M View Post
    Easist way to capitalise first letter:-


    Code:
    <script type = "text/javascript">
    var str = "jEAn-paul o'flaNAGan-macDONald is a               brilliant coder";
    str = str.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
    document.write(str); 
    </script>
    I agree that is the easiest way - especially if you make a String method out of that code. It doesn't demonstrate as many of the features of the RegExp object does as my example does though (and I did say that I wrote that code several years ago).

    The OP should now be able to gain a greater understanding of the use of regular expressions in JavaScript by comparing the three different variants of the code that are now in this thread. I believe that was the objective in their asking the question about using a RegExp to convert to uppercase in the first place.
    Last edited by felgall; 01-24-2012 at 08:03 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Well, whether written several years ago or not, it is more verbose and not as effective as my oneliner script, and as has been pointed out if there is more than one space between the words give undesirable results.

    Code:
    document.write("jEAn-paul o'flaNAGan-macDONald is a               brilliant coder".toTitleCase());
    results in

    Jean-paul O'flanagan-macdonald Is Brilliant Brilliant Coder

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •