sieja
sieja

Reputation: 17

unicode as string execute by variables

let testowy = v.za0+v._7a+v._1+v._1+v._13+v._3+v.za0+v._7a+v._1+v._1+v._13+v._12+v.za0+v._7a+v._1+v._1+v._13+v._11+v.za0+v._7a+v._1+v._1+v._14+v._5+v.za0+v._7a+v._1+v._1+v._14+v._10+v.za0+v._7a+v._1+v._1+v._5+v._15+v.za0+v._7a+v._1+v._1+v._5+v._16;

console.log(testowy) // output unicode "\u0061\u006c\u0065\u0072\u0074\u0028\u0029"
eval(testowy) // gives error
eval("\u0061\u006c\u0065\u0072\u0074\u0028\u0029") // OK - alert()

the question is how to make testowy variable to work as a string?

eval("'"+"testowy+"'") // or
eval("\""+"testowy+"\"") // doesn't work 

Upvotes: 0

Views: 104

Answers (1)

phuzi
phuzi

Reputation: 13059

if console.log is writing "\u0061\u006c\u0065\u0072\u0074\u0028\u0029" then the actual string was probably set using escaped slashes -

\\u0061\\u006c\\u0065\\u0072\\u0074\\u0028\\u0029.

You'll need to convert the string of hexadecimal character codes to Unicode characters...

let testowy = v.za0+v._7a+v._1+v._1+v._13+v._3+v.za0+v._7a+v._1+v._1+v._13+v._12+v.za0+v._7a+v._1+v._1+v._13+v._11+v.za0+v._7a+v._1+v._1+v._14+v._5+v.za0+v._7a+v._1+v._1+v._14+v._10+v.za0+v._7a+v._1+v._1+v._5+v._15+v.za0+v._7a+v._1+v._1+v._5+v._16;

console.log(testowy); // outputs \\u0061\\u006c\\u0065\\u0072\\u0074\\u0028\\u0029

testowy = testowy.split('\\u')
    .map(s => {
        // ignore leading empty string.
        if (s !== '')
            return String.fromCharCode(parseInt(s, 16));
    })
    .join('');

console.log(testowy); // outputs alert()

Upvotes: 1

Related Questions