简体   繁体   English

使用 JavaScript / 正则表达式替换>>(双大于符号)

[英]Using JavaScript / Regex to replace >> (double greater than signs)

I need " >> " to be replaced by "/" String: Test 2 >> Cat >> Cat2 Would become: Test 2/Cat/Cat2我需要将“>>”替换为“/”字符串:Test 2 >> Cat >> Cat2 将变为:Test 2/Cat/Cat2

Please know that I spent a ton of time reading all stack overflow and visiting fiddles but I can not get this right.请知道我花了很多时间阅读所有堆栈溢出和访问小提琴,但我无法做到这一点。 I used online Regex tools, https://regex101.com/ and https://regexr.com/ and they confirmed RegEx was fine.我使用了在线 Regex 工具https://regex101.com/https://regexr.com/ ,他们确认 RegEx 没问题。 Each of these has tested positive in Regex testing tools but I'm still not getting results I expect.这些中的每一个都在正则表达式测试工具中测试为阳性,但我仍然没有得到我期望的结果。 I can not share code but here is a demonstration.我不能分享代码,但这里有一个演示。

<span id="catidX"> Test 2 >> Cat >> Cat2 >> Cat4 || //// Cat /// Cat2 // Cat4 // CCat / CCat2 // CCat3 ///</span>
<script>

   var str = document.getElementById("catidX").innerHTML; 
    //var resA = str.replace(/[>]{2,}/g, "/");    - Attempt
    //var resA = str.replace([>]{2,}/g, "/");     - Attenmpt
    //var resA = str.replae(/>{2,}/g, "/");     - Attenmpt
    var resA = str.replace(/>>|>{2,}/g, "/");     
    var resB = resA.replace(/\/\//g, "/");
    document.getElementById("catidX").innerHTML = resB;
</script>

This code above produces the results上面的代码产生结果

Test 2 >> Cat >> Cat2 >> Cat4 >> Detail || -- Cat -/ Cat2 - Cat4 - || CCat / CCat2 - CCat3 -/

I was just passing it through to replace to see results in both parts of the string.我只是通过它来替换以查看字符串两个部分的结果。

UPDATE更新

  • As per one of the helpful comments from a user - I added console.log(resB) to see what was output... which is not > but html for > - &gt根据用户的有用评论之一 - 我添加了 console.log(resB) 以查看 output 是什么...这不是 > 而是 html 用于 > - &gt

GT hTML

With that info - I edited that line but still no success有了这些信息-我编辑了那行,但仍然没有成功

var resA = str.replace(/&gt;{2,}/g,"/");

try this code试试这个代码

var str = "Test 2 >> Cat >> Cat2 >> Cat4"
str.replace(/>>/gi, "/");

result : "Test 2 / Cat / Cat2 / Cat4"

You can use the following regular expression:您可以使用以下正则表达式:

/([>]{2})(?=[\s])+/g

Here is how I would do that: You will need to invoke replace() with a regex that matches globally all occurrences of >> using the g modifier:以下是我的做法:您需要使用一个正则表达式调用replace() ,该正则表达式匹配全局所有出现的>>使用g修饰符:

let newText = oldText.replace(/>>/g, "/");

 let btn = document.querySelector("#start"); let span = document.querySelector("#catidX"); btn.addEventListener("click", function() { let oldText = span.innerText; let newText = oldText.replace(/>>/g, "/"); span.innerText = newText; });
 <span id="catidX"> Test 2 >> Cat >> Cat2 >> Cat4 || //// Cat /// Cat2 // Cat4 // CCat / CCat2 // CCat3 ///</span> <br /> <button id="start">Replace</button>

 let str = 'Test 2 &gt;&gt; Cat >> Cat2 &gt;> Cat4 || //// Cat /// Cat2 // Cat4 // CCat / CCat2 // CCat3 ///'; res = str.replace(/(?:>|&gt;){2,}|\/\/+/g, '/'); console.log(res);

  • (?:>|&gt;){2,} is a non capture group that matches 2 or more "greater than" > or html entity &gt; (?:>|&gt;){2,}是一个非捕获组,匹配 2 个或多个“大于” >或 html 实体&gt;
  • \/\/+ matches 2 or more slashes \/\/+匹配 2 个或多个斜杠

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

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