JavaScript:查找嵌套[引用]

发布于 2024-10-04 03:30:57 字数 972 浏览 3 评论 0原文

我想用 JavaScript 在用户端进行表单验证(也使用 jQuery)。目标是删除比级别 2 更深的嵌套 bbCode [quote] 标记。比如说,我们有这样的文本:

[quote=SoundMAX][quote=Laplundik][quote=SoundMAX]
blahblahblah[/quote]
blahblah
[/quote]
blah[/quote]

并得到这个:

[quote=SoundMAX][quote=Laplundik]
blahblah
[/quote]
blah[/quote]

我唯一的想法是.replace [quote] 使用

,然后创建 DOM 对象并使用 jQuery 删除任何深度超过 2 的内容,并将所有内容向后解析为 bbCode。但这个解决方案似乎太复杂了,有没有更优雅的解决方案?

编辑:

感谢您提供好的解决方案。根据达里奥的回答,我这样做了:

var text=$('#edit-privatemsgbody').val();
var tmp=[];
var level=0;

for (var i=0,l=text.length;i<l;i++){
 if(text[i]=='['&&text[i+1]=='q') level++; 
 if(text[i-6]=='q'&&text[i-7]=='/'&&text[i-8]=='[') level--;
 if(level<3) tmp.push(text[i]);
}
alert(tmp.join(''));

效果很好。

但idealmachine的解决方案就像是一闪而过。以前我不知道替换回调函数参数,现在很方便!我会解决的。

I want to do form validation at user side with JavaScript (jQuery is also used). The goal is to remove nested bbCode [quote] tags deeper than level 2. Say, we have this text:

[quote=SoundMAX][quote=Laplundik][quote=SoundMAX]
blahblahblah[/quote]
blahblah
[/quote]
blah[/quote]

And get this:

[quote=SoundMAX][quote=Laplundik]
blahblah
[/quote]
blah[/quote]

My only idea is to .replace [quote] with <div>, then create DOM object and remove anything deeper than 2 with jQuery, and parse all backwards to bbCode. But that solution seems too complicated, are there more elegant one?

EDIT:

Thanks for nice solutions. Based on darioo's answer, I did this:

var text=$('#edit-privatemsgbody').val();
var tmp=[];
var level=0;

for (var i=0,l=text.length;i<l;i++){
 if(text[i]=='['&&text[i+1]=='q') level++; 
 if(text[i-6]=='q'&&text[i-7]=='/'&&text[i-8]=='[') level--;
 if(level<3) tmp.push(text[i]);
}
alert(tmp.join(''));

Which works just fine.

But idealmachine's solution was like a flash. I didn't know about replace callback function parameters before, now that is handy! I'll settle with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠佳期 2024-10-11 03:30:57

实际上,如果您将正则表达式视为无法处理嵌套本身的有限工具,则可以使用它。 .replace 字符串方法可以调用一个函数来查找每个匹配的替换文本,这使我们能够跟踪标记结构的深度(代码也发布在 http://jsfiddle.net/Zbgr3/3/):

var quoteLevel = 0;

alert(s.replace(/\[(\/?)quote[^\]]*\]|./gi, function(tag, slash) {
    // Opening tag?
    if(tag.length > 1 && !slash.length) quoteLevel += 1;
    // What to strip
    var strip = quoteLevel > 2;
    // Closing tag?
    if(tag.length > 1 && slash.length) quoteLevel -= 1;

    if(strip) return '';
    return tag;
}));

如果您希望对标记中的错误有一定的容忍度,您可以添加一些额外的代码,例如防止 quoteLevel 低于零。

Actually, you can use regex if you look at it as a limited tool that cannot handle the nesting itself. The .replace string method can call a function to find the replacement text for each match, which allows us to track how deep we are in the markup structure (code also posted at http://jsfiddle.net/Zbgr3/3/):

var quoteLevel = 0;

alert(s.replace(/\[(\/?)quote[^\]]*\]|./gi, function(tag, slash) {
    // Opening tag?
    if(tag.length > 1 && !slash.length) quoteLevel += 1;
    // What to strip
    var strip = quoteLevel > 2;
    // Closing tag?
    if(tag.length > 1 && slash.length) quoteLevel -= 1;

    if(strip) return '';
    return tag;
}));

If you want some tolerance for errors in the markup, you could add some extra code that, for example, prevents quoteLevel from falling below zero.

兔小萌 2024-10-11 03:30:57

使用常规数组作为堆栈。每次遇到 [quote] 时,请使用其 push() 方法将数组加一。当遇到 [/quote] 时,使用其 pop() 方法将数组减一。

如果遇到 [quote] 并且数组长度为 2,请删除该 [quote],并删除遇到的下一个 [/quote]

如果您没有相同数量的开盘报价和闭盘报价,那么您必须以您认为合适的方式进行处理。

Use a regular array as a stack. Every time you encounter [quote], increase your array by one using its push() method. When you encounter [/quote], decrease your array by one using its pop() method.

If you encounter [quote] and your array length is 2, remove that [quote], and remove the next [/quote] you encounter.

If you don't have the same number of open and closed quotes, then you'll have to handle that in a way you find appropriate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文