Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  108] [ 1]  / answers: 1 / hits: 30008  / 9 Years ago, thu, august 27, 2015, 12:00:00

It could be any string, it should match only the UPPERCASE part and change to lowercase, for example:



It's around August AND THEN I get an email



become



It's around August and then I get an email



as you can see the word It's, August and I should be ignored


More From » regex

 Answers
13

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(?!Ib)[A-Z]+b

[#65268] Tuesday, August 25, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daja

Total Points: 407
Total Questions: 103
Total Answers: 103

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
daja questions
Tue, Dec 21, 21, 00:00, 2 Years ago
Thu, Apr 23, 20, 00:00, 4 Years ago
Fri, Sep 6, 19, 00:00, 5 Years ago
Tue, Jul 23, 19, 00:00, 5 Years ago
;