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

157
Visualizações
Find JavaScript function with regex

take a look at my following regex: https://regex101.com/r/HMya54/2

I only want to find the javascript-function with "_tsid" in it, not all JavaScript functions, and to replace that whole function by an HTML-Comment <!-- here was my function -->.

I tried something like: /<script.*?>([\s\S]*?)_tsid([\s\S]*?)<\/script>/ but that seems not the right way.

Can you help me with that problem?

Thank you!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The simplest way is to use your current (or its optimized version, see below) and use a callback as the replacement argument where you can check if the match contains the string of your choice, and if it is there, perform the replacement:

let text = '&lt;script type="text/javacript">alert("First one!");&lt;/script>\n\n&lt;script>console.log("Hello World");&lt;/script>\n\n&lt;script type="text/javascript">\n                (function () {\n                    var _tsid = \'XFE5AF4B9ADDACA0E60017B1BC4D16AE2\';\n                    _tsConfig = {\n                        \'yOffset\': \'0\', /* offset from page bottom */\n                        \'variant\': \'reviews\', /* reviews, default, custom, custom_reviews */\n                        \'customElementId\': \'\', /* required for variants custom and custom_reviews */\n                        \'trustcardDirection\': \'\', /* for custom variants: topRight, topLeft, bottomRight, bottomLeft */\n                        \'customBadgeWidth\': \'\', /* for custom variants: 40 - 90 (in pixels) */\n                        \'customBadgeHeight\': \'\', /* for custom variants: 40 - 90 (in pixels) */\n                        \'disableResponsive\': \'false\', /* deactivate responsive behaviour */\n                                                \'disableTrustbadge\': \'false\', /* deactivate Trustbadge® */\n                                                \'responsive\': {\n                            \'variant\': \'\', /* floating, custom */\n                            \'customElementId\': \'\' /* required for variant custom */\n                        }\n                    };\n                    var _ts = document.createElement(\'script\');\n                    _ts.type = \'text/javascript\';\n                    _ts.charset = \'utf-8\';\n                    _ts.async = true;\n                    _ts.src = \'//widgets.trustedshops.com/js/\' + _tsid + \'.js\';\n                    var __ts = document.getElementsByTagName(\'script\')[0];\n                    __ts.parentNode.insertBefore(_ts, __ts);\n                })();\n&lt;/script>';
text = text.replace(/&lt;/g, '<').replace(/<script[^>]*>[^<]*(?:<(?!\/?script[\s>])[^<]*)*<\/script>/g, (x) => x.includes("_tsid") ? "<!-- here was my function -->" : x);
console.log(text);

The regex is now /<script[^>]*>[^<]*(?:<(?!\/?script[\s>])[^<]*)*<\/script>/g, it matches <script, anu zero or more chars other than >, then a >, then any zero or more non-< chars, then zero or more sequences of < not followed with an optional / and then script followed with a whitespace or > and then zero or more non-< chars, and then </script>, and then the text containing _tsid only is replaced, else, the match is reinserted into the resulting string.

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