Wikilinks - turn text [[a]] into an internal link

I need to implement something similar to wikilinks on my site. The user enters plain text and enters [[asdf]] wherever there is an internal link. Only the first five examples are really applicable in the implementation that I need.

Is it possible to use a regular expression, which expression will do this? Is there a library somewhere that already does this in C #?

+3
source share
3 answers

On the pure regex side, the expression would rather be:

\[\[([^\]\|\r\n]+?)\|([^\]\|\r\n]+?)\]\]([^\] ]\S*)
\[\[([^\]\|\r\n]+?)\]\]([^\] ]\S*)

(.+?) ([^\]\|\r\n]+?), , .

([^\] ]\S+) , wiki .

, #, .

, , pushdown automaton # regexp,

+2

, , , :

  • \[\[(.+?)\|(.+?)\]\](\S+) <a href="\2">\1\3</a>
  • \[\[(.+?)\]\](\S+) <a href="\1">\1\2</a>

- .

+1

, , - . , 90% , , , :

string html = "Some text with a wiki style [[page2.html|link]]";
html = Regex.Replace(html, @"\[\[([^\]\|\r\n]+?)\|([^\]\|\r\n]+?)\]\]([^\] ]\S*)", @"<a href=""$1"">$2$3</a>");
html = Regex.Replace(html, @"\[\[([^\]\|\r\n]+?)\]\]([^\] ]\S*)", @"<a href=""$1"">$1$2</a>");

- , , href , . .

+1

Source: https://habr.com/ru/post/1702222/


All Articles