在 JavaScript/HTML 中嵌套引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3039765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:03:26  来源:igfitidea点击:

Nesting quotes in JavaScript/HTML

javascripthtmlquotes

提问by Ryan Elkins

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?

如何在 HTML 中嵌套引号超出第二级?据我所知,只有两种类型的引号 - single(') 和 double(")。我知道使用斜杠转义 - 你必须在代码中转义,但转义在浏览器级别不起作用. 解决以下问题的公认方法是什么?

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

That code prints to the browser:

该代码打印到浏览器:

');">Some Text

');">一些文字

回答by Gumbo

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

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

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

Or in JavaScript using string escape sequences:

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

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

回答by jonS90

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

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

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

Link. It's "recursive escaping".

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

回答by Shabeer

    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);

回答by gruntled

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

这里有一些关于特殊字符的信息