Lenny Lenny - 4 months ago 9
Javascript Question

Can't figure out regex match, how far off am I?

Super basic question (which I admittedly probably shouldn't post on SO, but here goes): I'm trying to create a regex to match (and exclude from a function) all links that begins with

, but I cannot for the life of me get it to work.

Right now I have:


But since that clearly isn't correct, and I really don't know where to begin looking to set it straight, I'm asking for advice. I'm used to escaping strings etc., but this seems to be quite different(?).

Also, I tried using http://regexr.com/ and https://txt2re.com/ (didn't even understand how to work the latter...) but honestly I feel like I have no clue as to what to put where at this point since I'm basically guessing.

Again, sorry for posting such a basic question, but right now this is all gibberish to me, sadly.


This has little to do with jQuery or regular expressions. What you're asking about is a CSS selector which has different behaviour altogether. Your problem is with /\/\ vs \/\/.

Note that you don't actually have to escape the slashes here.

let images = document.querySelectorAll('a:not([href^="//images"])');
<a href="//images/cat.jpg">cat</a>
<a href="//images/dog.jpg">dog</a>
<a href="/images/fox.jpg">fox</a>
<a href="/images/bear.jpg">fox</a>