3
I’m going through the first column of the body of an HTML table and extracting the text from each of the cells using Javascript and testing against a Regex.
Regex has the function of identifying which sentences or strings (content of each cell) are formed exclusively by words with all letters capital letters, ignoring special characters, except accents in letters, punctuation marks and mathematical symbols.
I face difficulties in finding the correct Regex for the following sample cases in Javascript, which represent the whole properly. On the left, we have the text that is extracted from the table and, on the right, we have the result that should be returned by the function .test()
javascript:
(+/-) Provisão Despesas Administrativas - DPVAT ---------- false
PRÊMIOS EMITIDOS(-) PLANOS DE APOSENTADORIA -------------- true
(+) Prêmios - Riscos Vigentes Não Emitidos --------------- false
(+) OUTRAS RECEITAS E DESPESAS OPERACIONAIS -------------- true
(=) LUCRO LÍQUIDO / PREJUÍZO ----------------------------- true
CIRCULANTE ----------------------------------------------- true
(-) Redução ao valor recuperável ------------------------- false
Operações com Resseguradoras ----------------------------- false
ATIVOS DE RESSEGURO E RETROCESSÃO - PROVISÕES TÉCNICAS --- true
Prêmios Diferidos - PPNG --------------------------------- false
**********TOTAL DO ATIVO********** ----------------------- true
However, the Regex I include in the function isUpperCase()
below, does not correctly treat the above cases, considering true
, for example, the value "Prêmios Diferidos - PPNG"
, when this should be false
.
Function 1 below tests the string against Regex and Function 2 adds formatting to the text if all the words in the cell are all uppercase. Both are executed correctly, given the Regex limitation.
Function 1 - isUpperCase()
const isUpperCase = ( str ) => {
// RegEx contra a qual a palavra será testada
// FIX
return (/[A-ZÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ]{3}/).test(str);
}
Function 2 - addFormatting()
const addFormatting = ( table ) => {
let tableElement = document.querySelector('.' + table);
let cellCollection = tableElement.querySelectorAll('tbody tr > td:first-child');
cellCollection.forEach( function ( cell ) {
if( isUpperCase( cell.innerText ) ) {
cell.parentNode.setAttribute(
"style",
`color: rgb(0, 92, 169);
padding-left: -15px;
font-weight: bold;`
)
} else {
cell.style.paddingLeft = "32px";
}
});
}
I need help to find the appropriate Regex for the highlighted cases, which would be sufficient for my use case and my specific situation.
Excellent! I loved your explanation too! Thank you so much!
– user41210