Nesting quotes in JavaScript/HTML

后端 未结 4 820
青春惊慌失措
青春惊慌失措 2020-11-27 19:11

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

4条回答
  •  广开言路
    2020-11-27 19:52

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

提交回复
热议问题