正则表达式:将字符串与具有相同模式的子字符串匹配
Regex: Match string with substrings with the same pattern
我正在尝试将字符串与模式匹配,该模式可以包含具有相同模式的子字符串。
这是一个示例字符串:
Nicaragua [[NOTE|note|Congo was a member of ICCROM from 1999 and Nicaragua from 1971. Both were suspended by the ICCROM General Assembly in November 2013 having omitted to pay contributions for six consecutive calendar years (ICCROM [[Statutes|s|url|www.iccrom.org/about/statutes/]], article 9).]]. Another [[link|url|google.com]] that might appear.
这是模式:
[[display_text|code|type|content]]
所以,我想要的是获取括号内的字符串,然后寻找更多与顶层模式匹配的字符串。
我想要的是匹配这个:
- [[NOTE|s|note|Congo was a member of ICCROM from 1999 and Nicaragua from 1971. Both were suspended by the ICCROM General Assembly in November 2013 having omitted to pay contributions for six consecutive calendar years (ICCROM [[Statutes|s|url|www.iccrom.org/about/statutes/]], article 9).]]
1.1 [[Statutes|s|url|www.iccrom.org/about/statutes/]]
- [[link|s|url|google.com]]
我一直在使用这个 /(\[\[.*]])/
但它得到了最后一个 ]]
之前的所有内容。
我想要的是能够识别匹配的字符串并将它们转换为 HTML 元素,其中 |note|
将成为 blockquote 标记,而 |url|
将成为 a
标签。因此,块引用标记可以在其中包含 link 标记。
顺便说一句,我正在使用 CoffeeScript 来做到这一点。
提前致谢。
总的来说,正则表达式不擅长处理嵌套表达式。如果你使用贪婪模式,它们会匹配太多,如果你使用非贪婪模式,正如@bjfletcher 所建议的,它们会匹配得太少,停止在外部内容中。 "traditional" 这里的方法是一个基于标记的解析器,您可以在其中逐个逐个检查字符并构建一个抽象语法树 (AST),然后您可以根据需要重新格式化它。
我在这里使用的一种有点老套的方法是将字符串转换为 JSON 字符串,然后让 JSON 解析器完成转换为嵌套对象的艰苦工作:http://jsfiddle.net/t09q783d/1/
function toPoorMansAST(s) {
// escape double-quotes, as they'll cause problems otherwise. This converts them
// to unicode, which is safe for JSON parsing.
s = s.replace(/"/g, "\u0022");
// Transform to a JSON string!
s =
// Wrap in array delimiters
('["' + s + '"]')
// replace token starts
.replace(/\[\[([^\|]+)\|([^\|]+)\|([^\|]+)\|/g,
'",{"display_text":"","code":"","type":"","content":["')
// replace token ends
.replace(/\]\]/g, '"]},"');
return JSON.parse(s);
}
这为您提供了一个字符串数组和结构化对象,然后您可以通过格式化程序 运行 吐出您想要的 HTML。格式化程序留给用户作为练习 :).
我正在尝试将字符串与模式匹配,该模式可以包含具有相同模式的子字符串。
这是一个示例字符串:
Nicaragua [[NOTE|note|Congo was a member of ICCROM from 1999 and Nicaragua from 1971. Both were suspended by the ICCROM General Assembly in November 2013 having omitted to pay contributions for six consecutive calendar years (ICCROM [[Statutes|s|url|www.iccrom.org/about/statutes/]], article 9).]]. Another [[link|url|google.com]] that might appear.
这是模式:
[[display_text|code|type|content]]
所以,我想要的是获取括号内的字符串,然后寻找更多与顶层模式匹配的字符串。
我想要的是匹配这个:
- [[NOTE|s|note|Congo was a member of ICCROM from 1999 and Nicaragua from 1971. Both were suspended by the ICCROM General Assembly in November 2013 having omitted to pay contributions for six consecutive calendar years (ICCROM [[Statutes|s|url|www.iccrom.org/about/statutes/]], article 9).]]
1.1 [[Statutes|s|url|www.iccrom.org/about/statutes/]]
- [[link|s|url|google.com]]
我一直在使用这个 /(\[\[.*]])/
但它得到了最后一个 ]]
之前的所有内容。
我想要的是能够识别匹配的字符串并将它们转换为 HTML 元素,其中 |note|
将成为 blockquote 标记,而 |url|
将成为 a
标签。因此,块引用标记可以在其中包含 link 标记。
顺便说一句,我正在使用 CoffeeScript 来做到这一点。
提前致谢。
总的来说,正则表达式不擅长处理嵌套表达式。如果你使用贪婪模式,它们会匹配太多,如果你使用非贪婪模式,正如@bjfletcher 所建议的,它们会匹配得太少,停止在外部内容中。 "traditional" 这里的方法是一个基于标记的解析器,您可以在其中逐个逐个检查字符并构建一个抽象语法树 (AST),然后您可以根据需要重新格式化它。
我在这里使用的一种有点老套的方法是将字符串转换为 JSON 字符串,然后让 JSON 解析器完成转换为嵌套对象的艰苦工作:http://jsfiddle.net/t09q783d/1/
function toPoorMansAST(s) {
// escape double-quotes, as they'll cause problems otherwise. This converts them
// to unicode, which is safe for JSON parsing.
s = s.replace(/"/g, "\u0022");
// Transform to a JSON string!
s =
// Wrap in array delimiters
('["' + s + '"]')
// replace token starts
.replace(/\[\[([^\|]+)\|([^\|]+)\|([^\|]+)\|/g,
'",{"display_text":"","code":"","type":"","content":["')
// replace token ends
.replace(/\]\]/g, '"]},"');
return JSON.parse(s);
}
这为您提供了一个字符串数组和结构化对象,然后您可以通过格式化程序 运行 吐出您想要的 HTML。格式化程序留给用户作为练习 :).