Question

I am trying to code a fallback of getElementsByClassName with querySelectorAll for IE8.

The problem comes with classes which start with a number.

I know identifiers can't begin with a number, so querySelectorAll throws an error. But getElementsByClassName accepts them.

Then, is there a way of escaping those numbers?

Was it helpful?

Solution

I found the solution!

Identifiers can't start with a digit, but can start with an unicode escaped digit (see related answer).

Then, I can use

.replace(/\b\d/g, function(match){return '\\0000' + match.charCodeAt(0).toString(16);})

The code above escapes characters whose unicode code has two digits in hexadecimal. But for numbers, the following also works:

.replace(/\b\d/g, '\\00003$&')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top