...

View Full Version : Change Case



tqmd1
01-21-2012, 08:41 PM
Dear Experts

Var str="this is a string"

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

Please help

Logic Ali
01-21-2012, 09:31 PM
Sounds like a homework question, so I'll just recommend using String.replace with an external function:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter

Philip M
01-22-2012, 10:12 AM
What is wrong with .toUpperCase()?
Javascript is case sensiive so Var will thrrow an error.

felgall
01-22-2012, 09:41 PM
It is always better to use a built in method where possible rather than a regular expression.

var str="this is a string".toUpperCase();

tqmd1
01-23-2012, 03:21 PM
You are right sir but i think nothing is wrong to learn new techniques.

felgall
01-23-2012, 09:56 PM
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:


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(' ');
}

Logic Ali
01-24-2012, 05:00 AM
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:


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:

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());

Philip M
01-24-2012, 08:17 AM
Easist way to capitalise first letter:-



<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>

felgall
01-24-2012, 08:49 PM
Easist way to capitalise first letter:-



<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.

Philip M
01-24-2012, 09:11 PM
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.


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

results in

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum