Home > Net >  Find JavaScript function with regex
Find JavaScript function with regex

Time:02-10

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!

CodePudding user response:

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.

  • Related