我正在寻找能够在文本中找到短代码字符串的 ruby 兼容正则表达式。
正则表达式将能够识别以下字符串:
[shortcode][shortcode=value][shortcode key=value][shortcode=value]Text[/shortcode][shortcode key1=value1 key2=value2]Text[/shortcode][shortcode]Text[/shortcode]
修剪您的示例中提供的解决方案:
\[shortcode(.*?)\]((.*?)\[\/shortcode\])?
Where$1将得到第一个shortcode和最近的之间的所有内容],并且$3将是和之间的[shortcode...]文本[/shortcode]。
至于从正则表达式中获取任意长度和顺序的已解析属性列表,不,你不能这样做。您可能可以在以下内容中查找特定属性$1:
key=(.*?)\b
But to get the full list, you'll need to use other methods (perhaps the Shortcode API would help) to tokenize/parse $1.
那这个呢 ?
/\[(?<shortcode>([^\]]+))(.+?)?\](?:(.+?)?\[\/\k<shortcode>\])?/.match('[blah]whatever[/blah]')
# => #<MatchData "[blah]whatever[/blah]" shortcode:"blah">
(只是替换$shortcode为(?<shortcode>([^\]]+))您提供的链接的正则表达式和\k<shortcode>第二个)