Page 1 of 1
[SOLVED] Replace all Caps to Small using JS RegExp
Posted: Tue Jun 14, 2005 12:24 am
by anjanesh
Im trying to replace all capital letters to small letters in a string using JavaScript's RegExp.
Code: Select all
<script type="e;text/javascript"e;>
content = "e;This is A Sentence"e;;
cExp = /їA-Z]/g;
sExp = /їa-z]/;
document.write(content+"e;<br>"e;);
content = content.replace(cExp,sExp);
document.write(content);
</script>
Instead all caps get replaced with the word null.
Any solutions ?
Thanks
Posted: Tue Jun 14, 2005 5:03 am
by Syranide
are you sure replace is regex? and are you sure of your regex?
otherwise, I believe strings have a function "toLower" "lowercase" or something like that that would do the trick much better.
Posted: Tue Jun 14, 2005 5:34 am
by Chris Corbyn
Syranide wrote:are you sure replace is regex? and are you sure of your regex?
otherwise, I believe strings have a function "toLower" "lowercase" or something like that that would do the trick much better.
Yes replace() in JS does handle regex and yes there is a method tolower() or something like that for the job however

Posted: Wed Jun 15, 2005 7:06 pm
by feyd
usage: string.toLowerCase()
Posted: Thu Jun 16, 2005 8:21 am
by anjanesh
Thanks. Did not realize it was that easy.
Strange why the RegExp isnt working.
Posted: Thu Jun 16, 2005 8:55 am
by Chris Corbyn
anjanesh wrote:Strange why the RegExp isnt working.
The you've used replace() completely wrong, that's why that isn't working
Replace generally uses a regex pattern on the first parameter and then a string which includes the backreferences ($1, $2, $3 etc) for the second parameter. Here you've just put a regex in each of them.
JavaScript doesn't support the "e" modifier which is something you would need to do this too (but then you'd just be toLowerCase()'ing each uppercase letter so simply using toLowerCase() is much more sensible anyway)
