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

时间:2017-03-26 16:49:32

标签: arrays regex shortcode

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

这是短代码:

[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="AIzaSyDZ0ucX5SAiseQTn72Iw-An6fm0n2S71RA"]

description 属性中可以包含任何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

等其他属性。

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

1 个答案:

答案 0 :(得分:0)

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

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

以下是一个实例:https://regex101.com/r/DGaRCo/1

相关问题