javascriptregexdomxpathdocument.evaluate

Use document.evaluate with an XPath containing a Regex


I want to get all the "a" elements with the href attribute in this form: http(s)://any.example.com, where any can be a string containing just letters and/or numbers.

I'm new to Regex and XPath, so I can't get it right.

I figured out the Regex, but I'm not sure if it's 100% correct:

/(http|https)://+[A-Za-z0-9]+\.example+\.+com/

So the XPath would look like this:

document.evaluate("//a[@href='/(http|https)://+[A-Za-z0-9]+\.google+\.+com/']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

but it doesn't work.

I would appreciate if someone could help me.


Solution

  • As of today, looks like browsers currently does not support XPATH 2. Applying regex over attributes is only supported in XPATH 2.0

    You would want to apply regex after filtering for the elements using XPATH 1.0 (no regex), iterate over the elements & further filter the elements using JS level regex instead

    References:

    1. https://stackoverflow.com/a/21405499/211794
    2. https://stackoverflow.com/a/6282877/211794
    3. https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate#Browser_compatibility