Search code examples
javascriptescapingbookmarklet

Escaping characters in JavaScript using their ASCII codes


I am creating a bookmarklet and I have to avoid using double quotes ( " ) because I have to include the bookmarklet inside a HTML page and it's inside a string that uses double quotes:

<a href="javascript:...">

In some places I have to use the code replace(/".*/, '');

In sed, I can escape double quotes using \x22 (22 is the ASCII hex code for double quotes)

Is it possible to do something similar in JavaScript like replace(/\x22.*/, '');?


Solution

  • The same way you would use quotes in any HTML attribute's value.

    example

    <img src="..." alt="A picture of Bob &quote;Wild Man&quot; Johnson">
    

    so for you

    <a href="javascript:replace(/&quot;.*/, '');">