Am looking at https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector#Escaping_special_characters
and I can't understand this section:
// "#fooar" (\b is the backspace control character)
console.log('#foo\bar');
// Does not match anything
document.querySelector('#foo\bar');
console.log('#foo\\bar'); // "#foo\bar"
console.log('#foo\\\\bar'); // "#foo\\bar"
document.querySelector('#foo\\\\bar');
<div id="foo\bar"></div>
<div id="foo:bar"></div>
I'm a JavaScript beginner and don't understand why the 4 slashes, I found another similar question but I didn't understand the answers.
I tried to run document.getElementById on "foo\bar" and it works ok with 2 slashes which makes sense to me as you need an extra backslash to escape the first backslash but why an extra 3 when using querySelector?
I have looked at similar posts on here but the answers are not clear for me, I'm looking for a simple breakdown explanation of what's happening here and what the difference is between how getelementbyid works which I do understand (double backslash) and queryselector (which uses 4).