skip to Main Content

So I have a string and I want to take a part of the string, that it matches. For example:
stringToFind:
"<html><head><script src="http://example.com"></script>..."
I need to get the source of the script tag. I need to find all instances of the script tag in the string, and get the url of the source.

I was going to use String.prototype.replace(), that uses regular expressions, but you have to replace it with something and the result is the whole string.

2

Answers


  1. const sources = document.documentElement.innerHTML.match(/(?<=<script.+src=")[^"]+(?=".*>s*</scripts*>)/g);
    console.log(sources);
    <script 
      src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"
      type="text/javascript"
    ></script>
    <script defer src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    Login or Signup to reply.
  2. javascript is pretty good at handling HTML markup, so you probably don’t need a regex here.

    this should do the trick:

    //<!-- 
    var html_code = `<html>
    <head>
    <script src="https://code.jquery.com/jquery-3.7.0.slim.min.js"></script>
    <script src="/assets/script.js"></script>
    </head>
    <body>
      <script>
        var code = 'nope';
      </script>
      <p>Other stuff</p>
      <footer><script src="/assets/footer.js"></script></footer>
      </body>
    </html>`;
    // -->
    
    const parser = new DOMParser();
    const html_doc = parser.parseFromString(html_code, 'text/html');
    
    const script_tags = html_doc.querySelectorAll('script[src]');
    const sources = Array.from(script_tags).map((s) => s.getAttribute('src'));
    
    console.log(sources);

    if you need to extract script tags from the DOM in the browser, then you only need this:
    console.log( Array.from(document.querySelectorAll('script[src]')).map((s) => s.getAttribute('src')) );


    side note: // <!-- and // --> is there to make the jsfiddle run (which it wont reliably when containing html code in strings) as suggested by @InSync

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search