I am using regex to parse javascript code (using an ES parser, such as Esprima, is not an option for technical environment limitations).
The subject code (the code being parsed is):
(function() {
$('#3bf779cd').replaceWith("<div class=\'shows\'>\n<\/div>");
window.fadeItems();
}).call(this);
The value I am interested in is the first parameter of replaceWith
, which is a string literal. I am getting this variable using regex:
const subjectValue = subjectCode.match(/\.replaceWith\(((["'])(?:(?=(\\?))\3.)*?\2)/m)[1].slice(1, -1);
console.log(subjectValue);
The output of this is:
<div class=\'shows\'>\n<\/div>
How do I escape subjectValue
in a way that the output would be:
<div class='shows'>
</div>
Simply using unescape
has no effect.
If I am not mistaken, the question comes does to how to unescape this value:
console.log('"<div class=\\\'shows\\\'>\\\\n<\\\/div>"');
You're looking for eval
(yes you heard correctly):
text = document.querySelector('script').textContent; dqString = /"(\\\\.|[^"])*"/g s = text.match(dqString)[0] raw = eval(s); alert(raw);
<script> (function() { $('#3bf779cd').replaceWith("<div class=\\'shows\\'>\\n<\\/div>"); window.fadeItems(); }); </script>
The most idiotic way would be.
"<div class=\\'shows\\'>\\\\n<\\/div>".replace(/\\n/g, '').replace(/\\/g, '');
// "<div class='shows'></div>"
Smarter way would be to first unescape \\n's than check with regex if there are unescaped matches, and than escape those as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.