将短代码解析为数组的正则表达式

Regex to parse a shortcode into an array

我有一个简码。我想提取所有属性及其值并将它们放入一个数组中。

这是简码:

[res_map address="Yeronga QLD 4104, Australia" description="<img src='http://localhost/wordpress/wp-content/plugins/responsive-maps-plugin/includes/img/company.png'> {br} Yeronga QLD 4104, Australia {br} Phone:  0040 752 235 756" directionstext="(directions to our address)" icon="blue" iconsize="" style="2" scalecontrol="no" typecontrol="no" streetcontrol="no" locateme="no" zoom="13" zoomcontrol="no" draggable="yes" scrollwheel="no" searchbox="no" clustering="no" logging="no" poi="yes" fullscreen="no" width="100%" height="500px" maptype="roadmap" popup="no" center="" refresh="yes" key="private_key"]

描述 属性可以包含任何 HTML 内容。我需要从这个短代码创建一个数组,如下所示:

address=>Yeronga QLD 4104, Australia

description=><img src='http://local.....lia {br} Phone:  0040 752 235 756

directionstext=>(directions to our address)

icon=>blue

等等其他属性。

正确的正则表达式是什么?

以下解决方案仅在字符串中没有 " 时有效:

\s?\[?([^=]+)="([^"]*)"\]?
  • \s?\[?:空格和 [ 不应包含在键中,因此在实际组匹配之前匹配
  • ([^=]+)=:匹配每个字符最多为=
  • 的组
  • "([^"]*)":匹配"
  • 中的一个组
  • \]?:匹配一个可选的]

这是一个活生生的例子:https://regex101.com/r/DGaRCo/1