Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
Regex cleaning some html string

I am trying to parse some html from a website.

The html may contain some invalid html which cause that the parser are not able to parse the html.

this is my regex that I wrote

/(\[class\]((=)("|')?.*("|')))|(\[class\])|((\[id\]((=)("|')?.*("|')))|(\[id\]))/

This will remove all [class] and [id] attr

My above regex work fine with some html but not all example 1 that works

<div class="par fontsize-16" [class]="'par fontsize-' + fontsize"><p>the two of them left that everyone came back to their senses.</p>

but it dose not work with

      </div><span id="saved" hidden>Settings saved..</span><div class="clear"></div><div class="par fontsize-16" [class]="'par fontsize-' + fontsize"><p>It wasn't " until the two of them left that everyone came back to their senses.</p>

This is caused by the string It wasn't " which is removed.

I only want to remove the attr and its content and not the tags content

is it possible

Final solution

Thanx to It goldman I ended up with a solution. I am posting it incase someone needs it.

  cleanHTML(html: string, ...attrs: string[]) {
    attrs.forEach(attr => {
      var pos = 0
      while ((pos = html.indexOf(attr)) > -1) {
        var sep = null;
        var state = 0;
        if (html[pos + attr.length] === "=") {
          for (var i = pos + attr.length; i < html.length; i++) {
            var c = html.charAt(i);
            if (c == '=') {
              state = 1
              continue;
            }
            if (state == 1 && (c.trim() === '"' || c.trim() === "'")) {
              console.log(c.trim())
              sep = c;
              break;
            } else if (state === 1)
              break;
          }
        }

        if (!sep) {
          html = html.substring(0, pos) + html.substring(pos + attr.length + (state== 1 ? 1 : 0));
          continue;
        }

        var pos_q = html.indexOf(sep, pos);
        var pos_q2 = html.indexOf(sep, pos_q + 1);
        html = html.substring(0, pos) + html.substring(pos_q2 + 1)
      }
    });

    return html;
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

var src = `</div><span [class] [class][class] id="saved" [id]hidden>Settings saved..</span><div class="clear"></div><div class="par fontsize-16" [class]="'par fontsize-' + fontsize"><p>It wasn't " until the two of them left that everyone came back to their senses.</p><a [class]='another'>sasportas</a>`

function clean_str(src, attributes_to_remove) {
  attributes_to_remove.forEach(function(attr) {
    var pos
    while ((pos = src.indexOf(attr)) > -1) {
      var sep;
      var state = 0;
      for (var i = pos + attr.length; i < src.length; i++) {
        var c = src.charAt(i);
        if (c == '=') {
          state = 1
          continue;
        }
        if (state == 0 && c.trim()) {
          sep = null;
          break;
        }
        if (state == 1 && c.trim()) {
          sep = c;
          break;
        }
      }
      if (sep) {
        var pos_q = src.indexOf(sep, pos);
        var pos_q2 = src.indexOf(sep, pos_q + 1);
        src = src.substring(0, pos) + src.substring(pos_q2 + 1)
      } else {
        src = src.substring(0, pos) + src.substring(pos + attr.length)
      }
    }
  })
  return src;
}

console.log(clean_str(src, ["[class]", "[id]"]))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda