I want to write a script which adds in a string a backslash () everytime it finds a quotation marks or \r. It works but for ” and ‘, but I don’t know how to write it for \r. Could anyone help me? THANKS
<script type="text/javascript">
var test="Let\'s test if this \"works\" properly";
var escapedString = test.replace(/(['"])/g, "\\$1");
document.write(escapedString);
</script>
Output: Let\’s test if this \”works\” properly
Just include the \r in your square-bracket expression.
EDITED TO ADD EVENT-BASED INPUT SOLUTION:
Apparently what you really mean is you want to know how to add an event listener to an input and have it work on keypress.
And handle it like this:
Try something like that. Hope this helps.