Steve Harrison Steve Harrison - 1 year ago 115
Javascript Question

Escaping Strings in JavaScript

Does JavaScript have a built-in function like PHP's

) function to add backslashes to characters that need escaping in a string?

For example, this:

This is a demo string with
'single-quotes' and "double-quotes".

...would become:

This is a demo string with
\'single-quotes\' and

Answer Source

function addslashes( str ) {
    return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download