利用 ES6 模板函数特性安全生成正则表达式
const escapeRegex = s => (s || '').replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
const r = (strs, ...exps) => new RegExp(strs.raw.map((x, i) => x + escapeRegex(exps[i])).join(''));
利用方法:
const js = `}(__0x12345, 0x123));`;
const varName = '__0x12345';
const rotate = js.match(r`${varName}\s*,\s*(\w+)`)[1]; // "0x123"