【问题标题】:replace multiple characters that's inside of a bbcode tag only, and not effect any other text that is outside of the bbcode仅替换 bbcode 标记内的多个字符,而不影响 bbcode 之外的任何其他文本
【发布时间】:2019-08-07 18:37:44
【问题描述】:

所以我创建了这个函数来替换字符串中的特殊字符。主要目的是替换 BBCode 标记内的那些特殊字符([code=any]我要显示的代码 [/code]),但当时是否替换其余的并不重要BBcode 标记之外的字符串。但是现在我在替换 BBcode 标记之外的 HTML 标记时遇到了问题。所以我试图为此想出一个解决方案,但到目前为止还没有运气。

目标是替换以下内容中的特殊字符:

[code=any]some code inside of here[/code]

还应该提到,当我说代码=任何意思时,它可以是任何东西。它可以是 HTML、CSS、PHP、JS [a-z-A-Z-0-9]。

更像

[code=[a-z-A-Z-0-9]<h1>some html insode of the bbcode to be replace</h1>[/code]

我目前的功能很简单。只需要一些基本的正则表达式:

replaceSpecial : function(str) {

str = str.replace(/&/g, "&amp;");
str = str.replace(/>/g, "&gt;");
str = str.replace(/</g, "&lt;");
str = str.replace(/"/g, "&quot;");
str = str.replace(/'/g, "&#039;");

return str;
}

但是我将如何重写它,以便它只替换以下内容中的文本:[code=any]some code inside here[/code] 就是这样。如果有人对此有解决方案,那就太棒了。

感谢您的宝贵时间, 乔恩·W

【问题讨论】:

  • 你可以使用捕获组regex101.com/r/KGCWmq/1查看我更新的问题
  • 请考虑将我的答案标记为已接受,因为它对您有用,如果对您有帮助,请点赞。

标签: javascript regex bbcode


【解决方案1】:

您是否要提取条形码内的文本并应用此正则表达式, 您可以使用 exec 将替换应用于代码中的内容, 可以使用捕获组和反向引用 查看更多链接https://www.regular-expressions.info/refcapture.html

在这种情况下,只获取bbcode里面的文本并进行处理

let code = "[code=any]<h1>hello and bye</h1>e[/code]";
//extract only texto inside bbcode
let match = /\[code\=any\](.+)\[\/code\]/g.exec(code)[1];
//get <h1>hello</h1>e
let replacement = myObj.replaceSpecial(match);

https://regex101.com/r/KGCWmq/1

这只是你想得到,如果你想替换你可以使用替换功能。

var myObj = {
    replaceSpecial : function(str) {
        str = str.replace(/&/g, "&amp;");
        str = str.replace(/>/g, "&gt;");
        str = str.replace(/</g, "&lt;");
        str = str.replace(/"/g, "&quot;");
        str = str.replace(/'/g, "&#039;");
        return str;
    }
};
let code = "[code=any]<h1>hello and bye</h1>e[/code]";
let match = /\[code\=any\](.+)\[\/code\]/g.exec(code)[1];
let replacement = myObj.replaceSpecial(match);
let string = code.replace(/\[code\=any\](.+)\[\/code\]/,"[code=any]"+replacement+"[/code]")

更新

根据Wiktor Stribiżew的回答,可以验证正则表达式使得bbcode是任意的

myObj = {
  replaceSpecial : function(str) {
     return str.replace(/&/g, "&amp;")
               .replace(/>/g, "&gt;")
               .replace(/</g, "&lt;")
               .replace(/"/g, "&quot;")
               .replace(/'/g, "&#039;");
  }
}

var s = '[code="HTML"]<h1>Hello</h1>[/code]';
var regex = /(\[code=[^\]]*])([^]*?)(\[\/code])/gi;
console.log( s.replace(regex, function($0, $group_match1, $group_match2, $group_match3) { return $group_match1 + myObj.replaceSpecial($group_match2) + $group_match3; }) );

希望我对您有所帮助,如果不是您所期望的,请发表评论并编辑问题

【讨论】:

  • 好吧,如果它是一个完整的函数,并且在完成替换后它只是将整个值变为原来的值,那就太好了。我试图弄清楚如何使这项工作发挥作用,因此它只是一个完整的功能。不过,我确实相信这非常接近。
  • 这正是我想要做的。
  • 如果我错了,请纠正我,但这只有在 BBCode 完全是 [code=any]some text[/code] 时才有效,对吗?在这种情况下,这是行不通的。代码可以是 HTML、CSS、PHP、JS,所以当我说任何时,我的意思是任何人。
  • 您可以使用以下答案中提到的表达式,它分为三组 /([code=[^]]*])([^]*?)([\/code] )/gi
  • 我已经根据以下答案的优化更新了我的答案
【解决方案2】:

如果你没有嵌套的code标签你可以使用

/(\[code=[^\]]*])([^]*?)(\[\/code])/gi

或者,如果您发现此正则表达式性能存在问题,请将其替换为根据 unroll-the-loop principle 编写的等效项:

/(\[code=[^\]]*])([^[]*(?:\[(?!\/code])[^[]*)*)(\[\/code])/gi

查看regex demo(和the optimized regex demo)。

详情

  • (\[code=[^\]]*]) - 第 1 组:[code=,然后是除 ] 之外的任何 0+ 个字符,然后是 ]
  • ([^]*?) - 第 2 组:任何 0 个或多个字符,尽可能少(*? 是*非贪婪量词)
  • (\[\/code]) - 第 3 组:[/code] 子字符串。

像这样在String#replace 中使用它:

function replaceSpecial(str) {
  return str.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
}

var s = '[code="JS"]<p align="center">D&elete the "Go" \'icon\'.</h1>[/code]';
var regex = /(\[code=[^\]]*])([^]*?)(\[\/code])/gi;
console.log( s.replace(regex, function($0, $1, $2, $3) { return $1 + replaceSpecial($2) + $3; }) );

注意:如果您必须在 [code=[/code] 之间排除 [code=,则需要将正则表达式调整为

/(\[code=[^\]]*])([^[]*(?:\[(?!\/?code[\]=])[^[]*)*)(\[\/code])/gi

this regex demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多