在 JavaScript/HTML 中嵌套引号

发布于 2024-09-05 14:50:03 字数 285 浏览 2 评论 0原文

如何在 HTML 中嵌套超出第二层的引号?据我所知,只有两种类型的引号 - 单引号(')和双引号(")。我知道使用斜杠转义 - 你必须在代码中转义,但转义在浏览器级别不起作用绕过以下

<p onclick="exampleFunc('<div id="divId"></div>');">Some Text</p>

代码的

可接受方法是什么?

');">一些文本

How do you nest quotes in HTML beyond the second level? As far as I know, there are only 2 types of quotes - single(') and double("). I am aware of escaping using slashes - you have to escape in the code but that escaping won't work at the browser level. What is the accepted method to get around something like the following?

<p onclick="exampleFunc('<div id="divId"></div>');">Some Text</p>

That code prints to the browser:

');">Some Text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光是把杀猪刀 2024-09-12 14:50:03

您需要使用正确的转义/编码。在 HTML 中使用字符引用:

<p onclick="exampleFunc('<div id="divId"></div>');">Some Text</p>

或者在 JavaScript 中使用字符串转义序列:

<p onclick="exampleFunc('\x3Cdiv\x20id\x3D\x22divId\x22\x3E\x3C/div\x3E');">Some Text</p>

You need to use proper escaping/encoding. Either in HTML using character references:

<p onclick="exampleFunc('<div id="divId"></div>');">Some Text</p>

Or in JavaScript using string escape sequences:

<p onclick="exampleFunc('\x3Cdiv\x20id\x3D\x22divId\x22\x3E\x3C/div\x3E');">Some Text</p>
一生独一 2024-09-12 14:50:03

编辑:这不是 HTML 中 JavaScript 的解决方案,而是仅适用于 JavaScript。我的错...

eval('eval(\"eval(\\\"alert(\\\\\\\"Now I\\\\\\\\\\\\\\\'m confused!\\\\\\\")\\\")\")');

链接。这是“递归转义”。

Edit: this is not a solution for JavaScript in HTML, but for JavaScript only. My bad...

eval('eval(\"eval(\\\"alert(\\\\\\\"Now I\\\\\\\\\\\\\\\'m confused!\\\\\\\")\\\")\")');

Link. It's "recursive escaping".

绅士风度i 2024-09-12 14:50:03
    const _advanceEscapeCount = (escapeCount, level) => {
      const linearPosition = Math.log(escapeCount + 1) / Math.log(2);
      return Math.pow(2, (linearPosition + level)) - 1;
    };

    const deepNestQuotes = (str, level) => {
      for (let i = str.length - 1; i >=0 ; i--) {

        if (str[i] === '"') {

          const index = i;
          let count = 0;
          while (str[i - 1] === '\\') {
            count++;
            i--;
          }

          const firstPart = str.substr(0,index - count);
          const lastPart = str.substr(index,str.length);

          const escapedCount = _advanceEscapeCount(count, level);
          str = firstPart +  '\\'.repeat(escapedCount) + lastPart;
          //str = firstPart +  escapedCount + lastPart;
        }
      }

      return str;
    };



    deepNestQuotes("1st-level-begin \"2nd-begin   \\\"3rd-begin  \\\\\\\"4th level\\\\\\\"   3rd-end\\\"   2nd-end\" 1st-level-end", 1);

    deepNestQuotes("1st-level-begin \"2nd-begin   \\\"3rd-begin  \\\\\\\"4th level\\\\\\\"   3rd-end\\\"   2nd-end\" 1st-level-end", 2);

    const _advanceEscapeCount = (escapeCount, level) => {
      const linearPosition = Math.log(escapeCount + 1) / Math.log(2);
      return Math.pow(2, (linearPosition + level)) - 1;
    };

    const deepNestQuotes = (str, level) => {
      for (let i = str.length - 1; i >=0 ; i--) {

        if (str[i] === '"') {

          const index = i;
          let count = 0;
          while (str[i - 1] === '\\') {
            count++;
            i--;
          }

          const firstPart = str.substr(0,index - count);
          const lastPart = str.substr(index,str.length);

          const escapedCount = _advanceEscapeCount(count, level);
          str = firstPart +  '\\'.repeat(escapedCount) + lastPart;
          //str = firstPart +  escapedCount + lastPart;
        }
      }

      return str;
    };



    deepNestQuotes("1st-level-begin \"2nd-begin   \\\"3rd-begin  \\\\\\\"4th level\\\\\\\"   3rd-end\\\"   2nd-end\" 1st-level-end", 1);

    deepNestQuotes("1st-level-begin \"2nd-begin   \\\"3rd-begin  \\\\\\\"4th level\\\\\\\"   3rd-end\\\"   2nd-end\" 1st-level-end", 2);

薔薇婲 2024-09-12 14:50:03

您需要使用 \ 转义字符,

因此您的代码应该类似于

<p onclick="exampleFunc('<div id=\"divId\"></div>');">Some Text</p>

以下内容: 特殊字符

you need to escape the characters using the \

so your code should look like

<p onclick="exampleFunc('<div id=\"divId\"></div>');">Some Text</p>

Here is some info on Special Characters

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文