Search code examples
javascriptregexgreasemonkeytampermonkey

Regex on PageSource in JavaScript


I am trying to create basic extension that finds a simple code that is often referenced in different pages in source (inside script tags, inside html classes etc).

Is there a way to run regex over the source code of a page inside?

Specicially, I have tried the following without success:

var code = document.innerHtml.match(/(b|cd)ef/)[0]

Solution

  • Here is something you can do

    const matchingCode = /.*croiss.*\s*/.exec(document.querySelector('#code').innerText, 'gmi');
    
    console.log(matchingCode);
    <div id="code">
      <div>
        J'aime les croissants
      </div>
    
      <div>
        Moi j'aime les endives
      </div>
    </div>