替换符号对为HTML标签的正则表达式

huangapple go评论52阅读模式
英文:

Regex replacing couples of symbols by HTML tags

问题

I can help you with the translation. Here is the translated content:

我正在尝试构建一个正则表达式,用于将所有符号对"$$"替换为某个HTML标签,比如<someTag></someTag>

我使用了这个正则表达式,但它并不涵盖所有情况:

$$(\S[^\*]+\S)$$
'aaa $$123$$ c$ ddd'.replace(/$$(\S[^\*]+\S)$$/g, '<a1>$1</a1>') // works

'aaa $$123$$ c$ $$ddd$$'.replace(/$$(\S[^\*]+\S)$$/g, '<a1>$1</a1>') // doesn't work, should be 'aaa <a1>123</a1> c$ <a1>ddd</a1>'

希望这能帮助你。如果有其他问题,请告诉我。

英文:

I'm trying to build a regex to replace all the couples of symbols "$$" with some HTML tag, say, &lt;someTag&gt;&lt;/someTag&gt;.

I use this regular expression but it doesn't cover all the cases:

$$(\S[^\*]+\S)$$
&#39;aaa $$123$$ c$ ddd&#39;.replace(/$$(\S[^\*]+\S)$$/g, &#39;&lt;a1&gt;$1&lt;/a1&gt;&#39;) // works

&#39;aaa $$123$$ c$ $$ddd$$&#39;.replace(/$$(\S[^\*]+\S)$$/g, &#39;&lt;a1&gt;$1&lt;/a1&gt;&#39;) // doesn&#39;t work, should be &#39;aaa &lt;a1&gt;123&lt;/a1&gt; c$ &lt;a1&gt;ddd&lt;/a1&gt;&#39;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(&#39;aaa $$123$$ c$ ddd&#39;.replace(/$$(\S[^\*]+\S)$$/g, &#39;&lt;a1&gt;$1&lt;/a1&gt;&#39;)) // works

console.log(&#39;aaa $$123$$ c$ $$ddd$$&#39;.replace(/$$(\S[^\*]+\S)$$/g, &#39;&lt;a1&gt;$1&lt;/a1&gt;&#39;)) // doesn&#39;t work, should be &#39;aaa &lt;a1&gt;123&lt;/a1&gt; c$ &lt;a1&gt;ddd&lt;/a1&gt;&#39;

<!-- end snippet -->


</details>


# 答案1
**得分**: 1

这是一个用于替换标记的 JavaScript 函数。它会根据指定的分隔符 `$$` 将字符串拆分,然后根据当前索引是奇数还是偶数来添加开放标记 (`prefix`) 或闭合标记 (`suffix`),最后返回替换后的字符串。希望这有所帮助!

<details>
<summary>英文:</summary>

Not a regex solution, but this works. Explanation: Split the string using your delimiter (`$$`). Then create a new string `result` and insert each part of the array. Then check if the current index is odd or even and depending on that add either the opening tag (`prefix`) or the closing tag (`suffix`). I hope this helps!


&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    function replaceTag(string, delimiter, prefix, suffix){
      
      let parts = string.split(delimiter);
      let result = &#39;&#39;;
      
      for(let index = 0; index &lt; parts.length; index++){
      
        result += parts[index];
      
        if(index % 2 == 0 &amp;&amp; index &lt; parts.length - 1){
        
          result += prefix;
        
        }
        else if(index &lt; parts.length - 1){
        
          result += suffix;
        
        }
      
      }
      
      return result;

    }

    console.log(replaceTag(&#39;aaa $$123$$ c$ ddd&#39;, &#39;$$&#39;, &#39;&lt;my-tag&gt;&#39;, &#39;&lt;/my-tag&gt;&#39;));
    console.log(replaceTag(&#39;aaa $$123$$ c$ $$ddd$$&#39;, &#39;$$&#39;, &#39;&lt;my-tag&gt;&#39;, &#39;&lt;/my-tag&gt;&#39;));

&lt;!-- end snippet --&gt;



</details>



# 答案2
**得分**: 1

问题在于你的正则表达式匹配方式是“贪婪匹配”,这意味着它会匹配字符串中尽可能多的部分。要使其成为“非贪婪匹配”,你需要添加`?`作为“懒惰量词”。我建议使用以下正则表达式:

```javascript
const regex = /$$(\w+?)$$/g

它会匹配两个$符号,然后至少一个单词字符。

英文:

The problem here is that your regex matches greedily, meaning it matches the biggest part of the string it is able to match. To make it ungreedy, you have to add the ? "lazy quantifier". I suggest using a regex like this one:

const regex = /$$(\w+?)$$/g

It matches two $ signs and then at least one word character.

答案3

得分: 1

以下是已翻译的内容:

纯正则表达式解决方案如下:

$$((?:(?!$$).)+)$$

其中 ((?:(?!\$\$).)+) 部分(也称为受限贪婪标记)将匹配除了 $$ 之外的任何内容,并且你要查找的内容将被捕获在第一个组中,然后你可以轻松地将它放入你想要的标签中。

这个正则表达式甚至允许你捕获单个 $,如果它出现在内容中,而且即使你的内容跨越多行,也可以工作,你可以将 . 替换为 [\w\W] 或根据你的字符匹配需求将 . 更改为其他内容。

演示

英文:

The pure regex solution you can use is this regex,

$$((?:(?!$$).)+)$$

Where ((?:(?!\$\$).)+) part (aka tampered greedy token) will match anything except $$ and the content you are looking for gets captured in group1 and then you can easily place it in the tags like you want.

This regex will even allow you to capture a single $ if it is present in the content and will work even if your content is spread across multiple lines for which you can either replace . with [\w\W] or you can change . to something else as per your char matching needs,

Demo

答案4

得分: 1

你可以使用 /\$\$(\S+)\$\$/g

Segment Description
<pre>\$\$</pre> 匹配文字 "$$"...
<pre>(\S+)</pre> ...捕获一个或多个非空白字符...
<pre>\$\$</pre> ...然后匹配文字 "$$"

这确保了成功匹配需要在两组 "$$" 之间有任意数量的字符,而没有空格。

const str = 'aaa $$123$$ $$c$ $$x $$ $$ddd$$';
const rgx = /$$(\S+)$$/g;
const sub = '<a1>$1</a1>';

const res = str.replace(rgx, sub);

console.log(res);
英文:

You could use /\$\$(\S+)\$\$/g.

Segment Description
<pre>\$\$</pre> Match literal "$$"...
<pre>(\S+)</pre> ...capture one or more non-whitespace characters...
<pre>\$\$</pre> ...then match literal "$$".

This ensures that a successful match requires any number of characters adjacent to two sets of "$$" with no spaces.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const str = &#39;aaa $$123$$ $$c$ $$x $$ $$ddd$$&#39;;
const rgx = /$$(\S+)$$/g;
const sub = &#39;&lt;a1&gt;$1&lt;/a1&gt;&#39;;

const res = str.replace(rgx, sub);

console.log(res);

<!-- end snippet -->

答案5

得分: 1

The fastest way is to use the non greedy dot-all approach: /\$\$(.*?)\$\$/sg
https://regex101.com/r/upveAX/1
Using the Dot alone will always be faster since it doesn't rely on assertion or class structures, which add 3 times more overhead in performance.

Should you choose to not use the Dot approach, you run the risk of orphaned $$ in your text as the regex mainly expect pairs of $$..$$.

英文:

The fastest way is to use the non greedy dot-all approach: /\$\$(.*?)\$\$/sg
https://regex101.com/r/upveAX/1
Using the Dot alone will always be faster since it doesn't rely on assertion or class structures,
which add 3 times more overhead in performance.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(&#39;aaa $$123$$ c$ ddd&#39;.replace(/$$(.*?)$$/sg, &#39;&lt;a1&gt;$1&lt;/a1&gt;&#39;));

console.log(&#39;aaa $$123$$ c$ $$ddd$$&#39;.replace(/$$(.*?)$$/sg, &#39;&lt;a1&gt;$1&lt;/a1&gt;&#39;));

<!-- end snippet -->

Should you choose to not use the Dot approach, you run the risk of orphaned
$$ in your text as the regex mainly expect pairs of $$..$$.

huangapple
  • 本文由 发表于 2023年5月18日 02:45:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定