简体   繁体   English

使用正则表达式解析Javascript中的Wiki标记

[英]Parsing Wiki markup in Javascript using regex

I'm attempting to parse some Wiki-style markup using the Javascript Creole Wiki Markup Parser . 我正在尝试使用Javascript Creole Wiki Markup Parser解析一些Wiki样式的标记。 I'm attempting to extend the parser to parse div tags like so: 我正在尝试扩展解析器来解析div标签,如下所示:

Markup: << any_content_here << 标记:<< any_content_here <<
HTML: <div class="left">content</div > HTML: <div class="left">content</div >

Markup: >> any_content_here >> 标记:>> any_content_here >>
HTML: <div class="right">content</div> HTML: <div class="right">content</div>

Markup: ^^ any_content_here ^^ 标记:^^ any_content_here ^^
HTML: <div class="horz">content</div> HTML: <div class="horz">content</div>

The parser uses regular expressions to parse the markup but regex is definitely not my strong point, and as the js file has virtually no comments I'm finding it particularly difficult to edit. 解析器使用正则表达式解析标记,但是regex绝对不是我的强项,并且由于js文件几乎没有注释,因此我发现编辑起来特别困难。 I've posted on the guy's blog asking for some help with this but as the post is about 2 years old I'm not expecting to hear back anytime soon... 我已经在这个人的博客上发布了一些与此相关的帮助,但是由于该帖子大约有2年的历史,所以我希望不久后都不会收到回复。

Any help with customising this, or if someone could point out a javascript parser that already supports div's, it would be most appreciated. 关于自定义此设置的任何帮助,或者如果有人可以指出已经支持div的JavaScript解析器,将不胜感激。

If you don't care about nesting, you don't even need a regex. 如果您不关心嵌套,则甚至不需要正则表达式。 Simply replace " << " with " <div class='left'> " and so on. 只需将“ << ”替换为“ <div class='left'> ”等。

To allow for nesting, you will have to (1) change the markup so the end is different from the start (eg <L> content </L> ) and (2) run a regex as many times as there are levels. 为了允许嵌套,您将必须(1)更改标记,使结尾与开头有所不同(例如<L> content </L> ),并且(2)运行正则表达式的次数应与层数相同。 The regex (for the left div) would be: 正则表达式(用于左div)将是:

<L>(((?!</?L>).)*)</L>

And the replacement string: 和替换字符串:

<div class="left">$1</div>

Here's a function that will take care of parsing all levels: 这是一个将解析所有级别的函数:

function parseLeft(markup) {
  var regex = /<L>(((?!<\/?L>).)*)<\/L>/g;
  out = markup.replace(regex, '<div class="left">$1</div>');
  if (out.length == markup.length) {
    return out;
  } else {
    return parseLeft(out);
  }
}

Example in action: 实际示例:

> parseLeft('<L> Outer div <L>inner div</L>outer again </L>');
<div class="left"> Outer div <div class="left">inner div</div>outer again </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM