browser-bug
browser-bug

Reputation: 2111

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\';--'

Upvotes: 0

Views: 61

Answers (1)

Maheer Ali
Maheer Ali

Reputation: 36594

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 \'.

Upvotes: 0

Related Questions