简体   繁体   English

正则表达式替换无法在除 chrome 之外的任何浏览器上按预期工作

[英]Regex replace not working as intended on any browser except chrome

I'm trying to create a function which adds a styled span element around a regex match to create text highlighting within a code element.我正在尝试创建一个 function ,它在正则表达式匹配周围添加一个样式化的 span 元素,以在代码元素中创建文本突出显示。

I've managed to create a solution, however, it only works as intended on chrome or if the json form it not copied and pasted in. On other browsers such as firefox and edge, it styles text that I did not intend on styling.我已经设法创建了一个解决方案,但是,它只能在 chrome 上按预期工作,或者如果 json 形成它没有复制和粘贴。在其他浏览器上,如 firefox 和边缘,它 ZBC4150D023D3255136DB671D6 不打算在造型上使用的文本I have checked my regex using regex101 and everything seems fine.我已经使用 regex101 检查了我的正则表达式,一切似乎都很好。

<pre>
   <code spellcheck="false" id="code" class="json" contenteditable="true">{
    "Id": "12345",
    "Title": "JsonForm",
    "Number": 1,
    "Text": "Text123",
    "Description": []
}</code>
</pre>
<button class="styled-button" onclick="highlight()">style</button>

<script>
  function highlight() {
    let block = document.getElementById('code');
    let formattedText = block.innerHTML;
    var text = formattedText.replace(/(\<span.*?\>|\<\/span\>)/gm, '');

    text = text.replace(/(".*?") *?(?=\:)/gm, "<span style='color: #7fdbca;'>$1</span>")
    //text = text.replace(/("[^\"\<\/\>\\]*?")(?=\,|$)/gm, "<span style='color: #ecc48d;'>$1</span>")
    //text = text.replace(/([0-9]*?)(?=,|$)/gm, "<span style='color: #F78C6C;'>$1</span>")
    block.innerHTML = text;

  }

</script>

https://jsfiddle.net/JacobShore/1xnkejma/22/ On browsers other than chrome, If you click the style button with the preset json it works, however, when you copy and paste the json into the code element and click style it doesn't. https://jsfiddle.net/JacobShore/1xnkejma/22/ On browsers other than chrome, If you click the style button with the preset json it works, however, when you copy and paste the json into the code element and click style it没有。 I am not sure what is happening here.我不确定这里发生了什么。 I've commented out the other styled spans but they also have the same problem.我已经注释掉了其他样式的跨度,但它们也有同样的问题。 I've added json for testing below.我在下面添加了 json 进行测试。

{
    "Id": "12345",
    "Title": "JsonForm",
    "Number": 1,
    "Text": "Text123",
    "Description": []
}

The regex for targeting the key-names was not working properly.用于定位键名的正则表达式无法正常工作。 Also, since you're re-using the block variable, it's more efficient to save the selection.此外,由于您正在重新使用block变量,因此保存选择更有效。

 const block = document.getElementById('code'); function highlight() { block.innerHTML = block.innerHTML.replace(/(\<span.*?>|<\/span>)/gm, '').replace(/("[^"]*"):/gm, "<span style='color: #7fdbca;'>$1</span>:"); }
 <pre> <code spellcheck="false" id="code" class="json" contenteditable="true"> { "Id": "12345", "Title": "JsonForm", "Number": 1, "Text": "Text123", "Description": [] } </code> </pre> <button class="styled-button" onclick="highlight()">style</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM