Skip to content

Instantly share code, notes, and snippets.

@luizhenriquesoares
Last active April 8, 2018 02:37
Show Gist options
  • Save luizhenriquesoares/5256e8c2e417ca6d291c256558ba9775 to your computer and use it in GitHub Desktop.
Save luizhenriquesoares/5256e8c2e417ca6d291c256558ba9775 to your computer and use it in GitHub Desktop.
regex filters
const regexList = [
{
Estado: "PE",
Campo: "AIT",
Regex: /[A-Z]{2}\d{8}/g
},
{
Estado: "BA",
Campo: "Valor",
Regex: /\d{3}([.])\d{2}/g
}
];
const content = "E X T R A T O D E M U L T A S PLACA: JQY6120 MULTAS A PAGAR Data/Hora Infração: 24/10/2014 08:35 Nº de Controle: 153491299 JU00094978 N° AIT: Cód. Infração: 6050 Órgão Autuador: PREF. DE: BA - JUAZEIRO Descrição: AVANC SINAL VERM DO SEMAF Natureza: GRAVISSIMA - G5 Pontos: 7 Valor: 191.54 Local: AVENIDA ADOLFO VIANA SN Município: JUAZEIRO UF: BA Data Notificação: 13/11/2014 Data Recurso: 23/08/2016 Data Vencimento: 23/08/2016 Data/Hora Infração: 19/06/2017 08:57 Nº de Controle: 224246585 JZ00024521 N° AIT: Cód. Infração: 5452 Órgão Autuador: PREF. DE: BA - JUAZEIRO Descrição: ESTAC SOB FAIXA P/ PEDEST Natureza: GRAVE Pontos: 5 Valor: 195.23 Local: AVENIDA FLAVIANO GUIMARAES SEM NUMERO Município: JUAZEIRO UF: BA Data Notificação: 22/06/2017 Data Recurso: 03/10/2017 Data Vencimento: 03/10/2017";
const setRegex = (regexList, state, content) => {
const matches = [];
regexList.map((row, i) => {
const patt = new RegExp(row.Regex)
let m;
while ((m = row.Regex.exec(content)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === row.Regex.lastIndex) {
row.Regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match) => {
matches.push({
multa: i,
estado: row.Estado,
campo: row.Campo,
resultado: match
});
});
}
});
console.log(matches)
console.log("=======================================================================")
console.log("Filtando por estado")
console.log("=======================================================================")
console.log(matches.filter(( res ) => res.estado == state))
};
const state = "BA"
setRegex(regexList, state, content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment