Search code examples
javascripthexstring-comparison

Is there a way to compare "\x3C!--" with "<!--" and get 'true' in JavaScript?


In one of my projects I have to parse HTML pages and to check if each parsed "tag candidate" is in the predefined list of tag defining strings.

The problem emerges when for some reason the parser returns "\x3C!--" as the "tag candidate", so I get false when comparing it with "<!--" from the list.

Is there any way to get true comparing those strings?

UPDATE:

To clarify my question and to explain where the error occurs I add two screenshots from DevTools panel, Google Chrome, version 104.0.5083.0 (Official Build) dev (64-bit).

Paused in debugger 1

We can see that the value of nodeParse.tagOpening is changed almost immediately after it has been assigned. It's quite strange.

Paused in debugger 2

But it's even more strange that if I open properties of nodeParse object in Local Scope, the correct (initially assigned) value is shown for tagOpening.

So it seems the browser knows that both shown values are actually equivalent, but for some reason distinguishes them. Any comments?


Solution

  • After a deeper investigation I have revealed that "\x3C!--" == "<!--" is always true, with no extra efforts (see the screenshot below).

    We should keep in mind however that string "<!--" (and any longer one) is stored as is in a variable, but is transformed into "\x3C!--" when is stored in an object's property. At least in Google Chrome.

    So we should assign the object's property to a variable (like var s2 = ob.a;), if we want to get value "<!--" back for sure. It was not known to me, so I have made a mistake in my code.

    Paused in debugger 3