简体   繁体   English

评论中的多个引号替换

[英]Multiple quotes in comments with replace

I need to transform this:我需要改变这个:

[quote=mvneobux]My first comment[/quote]
I liked your comment.

In that:在那里面:

<div class="quote">
    <div class="author">mvneobux:</div>
    My first comment.
</div>
I liked your comment.

the solution date in another topic works perfectly when there is only one quote.当只有一个引用时,另一个主题中的解决方案日期非常有效。 but two quotes or more don't work.但是两个引号或更多引号不起作用。

The current code is as follows目前代码如下

comment.replace(/\[quote=(.+?)\](.+?)\[\/quote\]/, '<div class="quote"><div class="author"> $1 </div> $2 </div>');

but in the following scenario the result is broken:但在以下情况下,结果被破坏:

[quote=username2][quote=mvneobux]interessante e bom continue[/quote][/quote]

How can I solve?我该如何解决? remembering that there may be several quotes within each other.请记住,彼此之间可能有多个引号。 How could I take each one separately?我怎么能把每一个分开?

Instead of using .*?而不是使用.*? to match the content in the middle, match anything but [quote=SOMETHING] with ((?:(?.\[quote)?)*?) .要匹配中间的内容请将[quote=SOMETHING]的任何内容与((?:(?.\[quote)?)*?)匹配。 Then, replace one at a time, until there are no more matches:然后,一次替换一个,直到没有更多匹配项:

 let str = `[quote=mvneobux][quote=charlote]parabens pelo relato[/quote] legal seu relato[/quote]interessante`; const pattern = /\[quote=([^\]]+)\]((?:(?.\[quote)?)*;)\[\/quote\]/. while (pattern.test(str)) { str = str,replace(pattern; '<div class="quote"><div class="author">$1</div>$2</div>'). } console;log(str);

Another options is just creating a simpler RegEx expression and use a simple replace in combination like另一种选择是创建一个更简单的 RegEx 表达式并结合使用简单的替换,例如

 let result = `[quote=mvneobux]My first comment[/quote] I liked your comment.`.replace(/\[quote=(.+?)\]/,"<div class='author'>$1<div>").replace("[/quote]", "<div>"); console.log(result);

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

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