Skip to content Skip to sidebar Skip to footer

Parsing Wiki Templates Calls With Javascript

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

Solution 1:

Please look at this Q&A: How can I fix this wiki link parsing regular expression?

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

Update:

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 }}

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

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

Solution 2:

regex that assumes your wiki template has always 3 parts: update to exclude false match to template {{template|[[link|name]]}}

regex:       \{\{(.+?)\|[^\[]{2}(.+?)\|(.+?)[^\]]{2\}\}
replacment:  $1,$2,$3
input:       {{template|unnamed_parameter|param1=value1}}
output:      template,unnamed_parameter,param1=value1

it's a simple regex using reluctant quantifiers and escaping the "special" meaning of {}| using \ by including \{\{ \}\} to the regex you avoid matches on [[ ]] pattern.

Post a Comment for "Parsing Wiki Templates Calls With Javascript"