JSFiddle - React, Tailwind, and code Playground

by Preetha Srinivasan

HTML

<div id="%test%">

</div>

JavaScript

String.prototype.escapeSelector = function () {
    return this.replace(
        /([$%&()*+,./:;<=>?@\[\\\]^\{|}~])/g,
        '\\$1'
    );
};

var x = "%test%";

var escaped = x.escapeSelector();

alert($('#' + escaped).length);