Capitalize despite mistake?

Stuart

Member
A very common thing people do is to write the initials without spaces like "E.B. White" instead of "E. B. White".
Can we make the capitalize function capitalize both letters? (i.e. any letter after a period or space gets capitalized)

1702382311451.png
 
P

Pabbly13

Guest
Hey @Stuart !

Sure, you can use the "Code by Pabbly" module to create such a conversion-

1702386320308.png


You can refer to this video tutorial on how this works-


Do check and let us know about it.
 

Stuart

Member
Edit:
I think this works!

function convertString(str) {
str = str.toLowerCase();
let words = str.split(' ');
for (let i = 0; i < words.length; i++) {
let word = words;
word = word.charAt(0).toUpperCase() + word.slice(1);
word = word.replace(/(\.| |-)(\w)/g, function(match, p1, p2) {
return p1 + p2.toUpperCase();
});
word = word.replace(/\b(i|ii|iii|iv|v|vi|vii|viii|ix|x)\b/gi, function(match) {
return match.toUpperCase();
});
words = word;
}
str = words.join(' ');
return str;
}

let input = "4. Result : e.b. wHite E.b. white jr. SR eeee beee wHITE III";
let refinedString = convertString(input);
return refinedString;
 
Last edited:

Stuart

Member
this Capitalizes the name and splits it into parts as needed!
we can add another line for titles if necessary!

function convertString(str) {
str = str.toLowerCase();
let words = str.split(' ');
for (let i = 0; i < words.length; i++) {
let word = words;
word = word.charAt(0).toUpperCase() + word.slice(1);
word = word.replace(/(\.| |-)(\w)/g, function(match, p1, p2) {
return p1 + p2.toUpperCase();
});
word = word.replace(/\b(i|ii|iii|iv|v|vi|vii|viii|ix|x)\b/gi, function(match) {
return match.toUpperCase();
});
words = word;
}
str = words.join(' ');
return str;
}

function parseName(name) {
const words = name.split(' ');
const suffixes = ['jr', 'jr.', 'i', 'ii', 'iii', 'iv', 'v', 'vi', 'vii', 'viii', 'ix', 'x'];
let lastName = '';
let firstName = '';
let restName = '';
let suffix = '';
if (words.length > 0) {
if (suffixes.includes(words[words.length - 1].toLowerCase())) { suffix = words.pop();
}
if (words.length > 0) { lastName = words.pop();
}
if (words.length > 0) { firstName = words.shift();
}
if (words.length > 0) { restName = words.join(' '); } }
restName = restName+' '+lastName;
if (suffix != "") { restName= restName+" "+suffix;
}
return {name, lastName, firstName, restName, suffix, };
}

let input = "e.b. wHite jr.";
let refinedString = convertString(input);
return parseName(refinedString);
 
Top