10
I have a Javascript code to capitalize text that treats some exceptions.
However, I would like to address a few more, such as ignoring abbreviations, which would be to have a point before or after the first letter and ignore some Roman numerals.
Any suggestions? Follow my code so far:
function testaCapitalize() {
var texto1 = "alto dO cruzeiro";
var texto2 = "joão paulo II";
var texto3 = "N.S. aparecida";
var texto4 = "N.S. DAS GRAÇAS";
document.getElementById('resultado').innerHTML =
capitalize(texto1) +
"<br/>" +
capitalize(texto2) +
"<br/>" +
capitalize(texto3) +
"<br/>" +
capitalize(texto4);
}
function capitalize(texto) {
texto = texto.toLowerCase().replace(/(?:^|\s)\S/g, function(capitalize) {
return capitalize.toUpperCase();
});
//preposição digitada
var PreposM = ["Da", "Do", "Das", "Dos", "A", "E", "De", "DE"];
//preposição substituta
var prepos = ["da", "do", "das", "dos", "a", "e", "de", "de"];
for (var i = PreposM.length - 1; i >= 0; i--) {
texto = texto.replace(RegExp("\\b" + PreposM[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "\\b", "g"), prepos[i]);
}
return texto;
}
<input type="button" onclick="testaCapitalize()" value='Testa'>
<div id='resultado'>
</div>
function capitalize(texto) {
texto = texto.toLowerCase().replace(/(?:^|\s)\S/g, function(capitalize) {
return capitalize.toUpperCase();
});
//preposição digitada
var PreposM = ["Da", "Do", "Das", "Dos", "A", "E", "De", "DE"];
//preposição substituta
var prepos = ["da", "do", "das", "dos", "a", "e", "de", "de"];
for (var i = PreposM.length - 1; i >= 0; i--) {
texto = texto.replace(RegExp("\\b" + PreposM[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "\\b", "g"), prepos[i]);
}
return texto;
}
By the array items, I suppose you are from Juazeiro/BA, Marlucio! Your question, the answers and comments here helped me a lot! Thanks!
– winiercape