简体   繁体   中英

Can someone explain me this behaviour?

Why in the first case there're backslashes while in the second one there is? The escape function shouldn't change anything right? And even if it was the most logic would be str.replace('\\'', '\\\\\\'') , so... Thanks in advance.

 escape = function(str) { str = str.replace('\\\\', '\\\\\\\\') str = str.replace('\\'', '\\\\\\'') str = str.replace('\\"', '\\\\\\"') str = str.replace('\\0', '') str = str.replace('\\r', '\\\\r') str = str.replace('\\n', '\\\\n') return str; } var original = ("Maura';--"); var escaped = escape("Maura';--"); //var encoded = btoa(escaped); console.log(original); console.log(escaped); //console.log(encoded);

Output:

'Maura';--'

'Maura\\';--'

In the first case you are not apply the escape function on the string original . In the second case its changed due to second line of the escape function

str = str.replace('\'', '\\\'')

The above line is same as

str = str.replace("'", '\\\'').

And the second part \\\\\\' will become \\' .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM