猿问

将所有英文引号替换为德文引号

我来这里是想问,是否有人可以帮助我进行引用和语法检查。

这是一个例子,完全集成到网站中,虚心请教,是否有人能找到错误?

function rewriteQuotes(){

  var all_p = document.querySelectorAll(`.content_container`);

  var regex_s = /[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]/;

  for (var i = 0; i < all_p.length; i++) {

    if (all_p[i] != null) {

      var all_nodes = all_p[i].querySelectorAll("*");

      for (var k = 0; k < all_nodes.length; k++) {

        if (all_nodes[k].childNodes.length > 0) {

          var all_children = all_nodes[k].childNodes;

          for (var t = 0; t < all_children.length; t++) {

            if (all_children[t].nodeName == "#text") {

              all_children[t].textContent = all_children[t].textContent.replace(/"/g,"\&ldquo;");

              all_children[t].textContent = all_children[t].textContent.replace(/\s\&ldquo;/g,"\ &bdquo;");

              if (all_children[t].textContent.charAt(0)== "\&ldquo;") {

                if (all_children[t].textContent.charAt(1) === "") {

                } else {

                  var char_1 = all_children[t].textContent.charAt(1);

                  if (regex_s.test(char_1)) {

                  } else {

                    all_children[t].textContent = "\&bdquo;" + all_children[t].textContent.substring(1);

                  }

                }

              }

            }

          }

        }

      }

    }

  }

};

document.addEventListener('DOMContentLoaded',function(){

  setTimeout(function(){

    rewriteQuotes();

  },1);

});

<div class="content_container">

  <h2>"Hello"</h2>

  <p>my "aunti" was driving over the "cat" with her "<em>skateboard</em>".</p>

</div>


元芳怎么了
浏览 154回答 1
1回答

慕婉清6462132

您可以简化正则表达式并捕获组"...",然后将这些组的引号替换为您想要的引号:const regex = /"(.*?)"/g;const str = `my "aunti" was driving over the "cat" with her "car"`;const subst = `„$1“`;const result = str.replace(regex, subst);console.log('Original: ', str);console.log('Result: ', result);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答