使用Javascript解析Wiki模板调用 [英] Parsing wiki templates calls with Javascript

查看:85
本文介绍了使用Javascript解析Wiki模板调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所需要做的就是将Wiki模板调用拆分为参数部分.在最基本的情况下,它只是被|拆分,因此{{template|unnamed_parameter|param1=value1}}将被拆分为{{templateunnamed_parameterparam1=value1}}.

All that I need is to split the wiki template call to parameter parts. In the very basic scenario it is just splitting by | so {{template|unnamed_parameter|param1=value1}} would be split to {{template, unnamed_parameter, param1=value1 and }}.

但是当将竖线字符用于其他目的(例如Wikilinks [[link|title]]等)时,事情就变得复杂了.

But things are complicating when the pipe character is used for other purposes like for wikilinks [[link|title]] etc.

任何建议如何以最简单的方式完成此任务? :)

Any suggestions how to do this task in the easiest way? :)

更新:很抱歉可能引起误会,但{{template|unnamed_parameter|param1=value1}}只是一个例子.有关Wiki模板的更多信息,请查看以下资源: http://www.mediawiki.org /wiki/Help:模板

Update: Sorry for possible misunderstanding but {{template|unnamed_parameter|param1=value1}} is just an example. For more information about wiki templates you can look at the following resource: http://www.mediawiki.org/wiki/Help:Templates

推荐答案

请查看以下问答:我的答案(在更新"部分中)使用perl regex进行了非常相似的Wiki链接解析.

My answer (in Update section) there using perl regex is doing pretty much similar Wiki link parsing.

好的,这是适合您的情况的perl正则表达式:

Alright here is the perl regex for your case:

echo "{{template|unnamed_parameter|param1=value1}}" |  \
perl -pe 's#(^|\b)((?![|\[]){{(.+?)\|(.+?)\|(.+?)}}(?![|\]]))($|\b)#{{$3, $4, $5 and }}#g'

Output: {{template, unnamed_parameter, param1=value1 and }}

问::确定要在关闭}}之前在这里需要and,否则只需在正则表达式上方进行

Q: are you sure you need and here before closing }} otherwise just edit above regex:

现在检查上述解决方案是否针对字符串[[link|title]]

And now checking above solution against string [[link|title]]

echo "[[link|title]]" |  \
perl -pe 's#(^|\b)((?![|\[]){{(.+?)\|(.+?)\|(.+?)}}(?![|\]]))($|\b)#{{$3, $4, $5 and }}#g'

Output: [[link|title]] # remains unchanged as per your requirements

这篇关于使用Javascript解析Wiki模板调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆