I have developed a regular expression that identifies all instances of two-letter words. The results can either be null or an array with an unknown number of items.
const regex_all_two_letter_words = /(\b\w{2}\b)/g;
var regexCapitalize = /[A-Z]+/g;
// inputs
const testString = [
"j handcock",
"je handcock",
"jim handcock",
"jim j handcock",
"jim je handcock,",
"jim jer handcock",
"j handcock",
"j. e, handcock",
"j. er handcock jr",
"jim js handcock sr",
"jim handcock nn",
];
testString.forEach((str) => {
var test = str.match(regex_all_two_letter_words);
console.log(test);
});
Results:
null
[ 'je' ]
null
null
[ 'je' ]
null
null
null
[ 'er', 'jr' ]
[ 'js', 'sr' ]
[ 'nn' ]
I am attempting to capitalize both letters and replace any lowercase variants with uppercase versions.
This is my current approach:
/// Inside the forEach function
var finalStr = function (test) {
if (test !== null) {
var text = test.replace(regexCapitalize);
return text;
}
return text;
};
(This returns various functions)
Thank you!