Put a regex inside a JavaScript template literal and every backslash in front of a character that isn't a string escape vanishes. \s turns into s. \d turns into d, \. into a bare dot, and \b into a backspace control character, which is worse. Whatever runs the string later gets a different regex from the one you typed. It still compiles, so you get no error, only a pattern that never matches. Double every backslash (\\s) or tag the literal with String.raw, and the problem goes away.
We know this the expensive way. It hid five rows of our detection catalogue, and every one of them read as a problem with the site.
What the page received
run-detection-tests.mjs is the script that opens each of our catalogue's detector pages in a profile and reads the verdict off it. A row's probe is a few lines of code that find the verdict text, and the probes live inside template literals in that one file, because they travel to the page as a string. (It is the same runner that once left about 30 tabs open, until each test got a fresh tab.) The BrowserScan bot row had this line, written the way you would write it in a .js file:
const m = t.match(/Test\s+Results?:?\s*([A-Za-z ]{3,30})/i)
Run that line through a template literal in Node and print it, and this is what the page gets:
const m = t.match(/Tests+Results?:?s*([A-Za-z ]{3,30})/i)
Tests+ now means "Test followed by one or more letter s". No page on earth prints that. The page in question printed Test Results: Normal in plain text, and the probe reported "Headline missing, 4/4 check cards rendered". The row came back unread.
unread is an honest status in our catalogue. It means the extractor found no verdict, which usually means the site changed its layout or stopped rendering one. So the status sent the next person to look at the site, which had nothing wrong with it.
Everything the window does, from a script on your own computerIn Mosaic: REST API. Everything the window does, from your own scripts. Free covers 3 profiles with no card, and Pro is $25 a month for any number of them.
Mosaic is a desktop app
Email yourself the link and open it on your computer.
Five rows, and one that was not even JavaScript
Five rows were in that state: browserscan-bot, creepjs-prototype, creepjs-workers, fingerprint-scan and coveryourtracks. Four were the same eaten \s. The creepjs-prototype row is the one that reads CreepJS's prototype page, which names every property a spoof has tampered with, so for as long as it was unread we had no reading from one of the most useful pages in the catalogue.
creepjs-workers had it worse. Somewhere in its probe sat '\n', quoted with single quotes. Unlike \s, \n is an escape the template literal knows, so it became a real newline, splitting a single-quoted string across two lines. JavaScript won't accept that. The page threw the entire probe away.
What made all five hard to see on review is that the older extractors in the same file had their backslashes doubled correctly. Reading the file, every regex looked like a regex. The difference between \s and \\s is one character in a line that looks right either way.
Some escapes do fail loudly. Template literals refuse octal-looking escapes, so a backreference such as \1 is a syntax error in the source file and Node stops on it. That is the lucky case. The quiet escapes are the ones that ship.
The check that runs every probe the way the page does
npm run check:extractors now runs inside our build, and it checks two things because there are two ways to be wrong.
- A lone
\s,\d,\b,\w,\/or\.inside a probe. None of those means anything as a string escape, so each one is always a\\that was not typed. This is an exact rule, not a guess. - Every probe, evaluated as the page would receive it, is passed to
new Function. That is the same parse the page does and nothing more, so a probe like the'\n'one fails the build instead of failing in a browser.
The checker had three bugs of its own, all found by its --self-test mode, which feeds it known faults and known good code:
- It tracked quotes, and the comments in our scripts are prose with apostrophes.
doesn'topened a "string" that ran to the next apostrophe, and the checker reported eighteen faults against an ordinary line in a different file. Comments are skipped now; ordinary strings are not, because skipping them loses track of where the file is. - It parsed the source text instead of the string's value. A correct
\\/in the source is\/in the value, but read raw it looked like a regex closing early. A checker that fails the fix it asks for gets switched off. - It treated literals with
${…}in them as probes. Those are messages being put together, not scripts sent to a page whole.
What the rows said once they parsed
With the probes fixed, four of the five rows passed. The fifth, creepjs-workers, started reporting that the page and its workers disagreed, and it took three more measurements to turn that into a fact. Two of the mismatches were the extractor's own: it compared fields against the word unknown that CreepJS prints for a field a realm lacks, and it compared stack depth, which is smaller in a worker in every browser. With those two removed, 12 disagreements in 57 fields became 3 in 45, all canvas.
A stock Chrome agreed with itself in the page and in all three kinds of worker. Ours did not, and the cause was real: the page's toBlob and convertToBlob were returning the canvas without its noise, byte-identical to stock, so the canvas defence could be read around by calling a different method. A change in our engine closed it, and the row now reads "3 worker realms agree with the window across 45 fields" (CreepJS's word for a page or a worker is a realm). The extractor had been right once it parsed. We would not have seen any of it while the probe was failing to parse.
Here's why it stung. A broken probe and a site that shows no verdict look identical in our table, yet only the second tells you anything about the site. We had a mirror image of this with a retired detector that still answers 200. The whole catalogue, 49 rows in five groups, is described in forty-nine detectors.
How to keep the backslash in a regex inside a template literal
There are three workable habits. Mixing them in one file is how we got here, so choose one:
- Doubling. Write
/Test\\s+Results?/and the page gets/Test\s+Results?/. Ugly, but obvious once everyone does it. String.raw. The tag keeps what you typed, single backslashes included. You still have to escape a backtick.- Not putting the regex in a string at all. Ship its
sourceand flags as data, or keep the probe as an ordinary function in an ordinary file and sendfn.toString().
And whichever one you use, test the string by parsing it. Your editor and TypeScript treat the probe as one opaque string; to them it's text, not code. Nothing reads it as JavaScript until the page does, which is too late to find out.






