57755

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 split to {{template, unnamed_parameter, param1=value1 and }}.

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? :)

<strong>Update:</strong> 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

Answer1:

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

<strong>Q:</strong> 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

Answer2:

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
</pre>

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.

Recommend

  • Does Outlook.com support EWS?
  • Referring to Child Properties in Ractive.js
  • x86 multi-byte NOP and instruction prefix
  • How do I get bash to redirect stderr into a >( command substitution )?
  • How can I extract columns from a fixed-width format in Perl?
  • Why is my event listener written in Google Closure not working?
  • javascript XSLT nodes, selecting the first of a group (merge-like)
  • Is there a way to query who are activesync/bb users through Exchange PowerShell?
  • WPF MVVM cancel window closing
  • How to record a JNLP/ Java Web Start application with JMeter
  • How to make nicEditor snaplet? (Several questions)
  • How many Vertica Databases can run on a Host in the same time?
  • CRASH: *** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds [0 .. 9]
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • In Java, how can I construct a File from a resource?
  • How can I replace the server in Web Component Tester
  • Sending keystrokes/mouse clicks to a Java program with Autohotkey
  • converting text file into xml using php?
  • JBoss External Properties Files in Classpath
  • Unable to get column index with table.getColumn method using custom table Model
  • custom UITableViewCell with image for highlighting
  • How to match http request and response using Jersey ContainerRequestFilter and ContainerResponseFilt
  • Launch Runnable Jar from Web Start
  • Different response to non-authenticated users and AJAX calls
  • How would I use PHP exceptions to define a redirect?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Knitr HTML Loop - Some HTML output, some R output
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Web-crawler for facebook in python
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Invalid access key error using credentials redeemed from an amazon open id token
  • Unanticipated behavior
  • bootstrap to use multiple ng-app
  • retrieve vertices with no linked edge in arangodb
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to get icons for entities from eclipse?
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Understanding cpu registers
  • Turn off referential integrity in Derby? is it possible?
  • How does Linux kernel interrupt the application?
  • Add sale price programmatically to product variations
  • JaxB to read class hierarchy