how to change uppercase to lowercase with regex javascript
A solution without regex:
function isUpperCase(str) {
return str === str.toUpperCase();
}
var str = "It's around August AND THEN I get an email";
var matched = str.split(' ').map(function(val){
if (isUpperCase(val) && val.length>1) {
return val.toLowerCase();
}
return val;
});
console.log(matched.join(' '));
Use /\b[A-Z]{2,}\b/g
to match all-caps words and then .replace()
with a callback that lowercases matches.
var string = "It's around August AND THEN I get an email",
regex = /\b[A-Z]{2,}\b/g;
var modified = string.replace(regex, function(match) {
return match.toLowerCase();
});
console.log(modified);
// It's around August and then I get an email
Also, feel free to use a more complicated expression. This one will look for capitalized words with 1+ length with "I" as the exception (I also made one that looked at the first word of a sentence different, but that was more complicated and requires updated logic in the callback function since you still want the first letter capitalized):
\b(?!I\b)[A-Z]+\b