cloudflare / templates

A collection of starter templates and examples for Cloudflare Workers and Pages
https://cloudflareworkers.com
MIT License
1k stars 638 forks source link

RegEx pattern for whole script tag #47

Closed odonline closed 1 year ago

odonline commented 5 years ago

on examples/third-party-scripts/third-party-scripts.js there is a combination regex patterns to get script tags

const SCRIPT_PRE = '<\\s*script[^>]+src\\s*=\\s*[\'"]\\s*((https?:)?/';
const PATTERN_POST = '[^\'" ]+)\\s*["\'][^>]*>';

and on the function modifyHtmlStream

for (let scriptUrl of SCRIPT_URLS) {
    let regex = new RegExp(SCRIPT_PRE + scriptUrl + PATTERN_POST, 'gi');
    patterns.push(regex);
  }

there is a problem with the combination of RegExp patterns because that can´t get the whole script tag on an html fetched file <script src="https://www.googletagservices.com/tag/js/gpt.js"></script>

so i made a new one so you can test it and look if it´s a better one

const htmlTagWithScriptsGPTGoogle="";
const patternGptTag = '<\\s*script[^>]+src\\s*=\\s*[\'"]\\s?(https:\/\/www\.googletagservices\.com\/tag\/js\/gpt\.js)*([^\'" ]+)\\s*["\'][^>]*><\/script>'

                const jsRegex = new RegExp(patternGptTag, 'gi');
                let match = jsRegex.exec(htmlTagWithScriptsGPTGoogle);                
                console.log(match[0])

example regexp on this link https://regex101.com/r/csy1QX/2

the patter offcourse could be better but the one with the scripting example didn´t worked for me.

lauragift21 commented 1 year ago

Hi @odonline! Thanks for creating this issue. The templates repo has moved to workers-sdk monorepo. I'll close this issue, but feel free to open a pull request to update the regex script in the worker-sdk repo. I can also investigate and add it for you.