在wordpress标签上运行的正则表达式

时间:2017-07-19 05:58:01

标签: javascript regex

我正在调用wordpress API并获取帖子的内容,但它中有标签,我正在尝试过滤它们以获取实际的HTML,这是一个示例:

   <p>[vc_row css=&#8221;.vc_custom_1499904320526{margin-top: 0px 
   !important;border-top-width: 0px !important;padding-top: 5px 
   !important;}&#8221;][vc_column][vc_custom_heading 
   source=&#8221;post_title&#8221; 
   font_container=&#8221;tag:h2|text_align:center&#8221; 
   use_theme_fonts=&#8221;yes&#8221;][vc_column_text]<strong>START 
   HERE</strong></p>\n<p>Release Date:<br />\nVersion:</p>\n<p>I am text 
   block. Click edit button to change this text. Lorem ipsum dolor sit amet,
    consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper 
   mattis, pulvinar dapibus leo.</p>\n<p>[/vc_column_text][/vc_column]
   [/vc_row][vc_row][vc_column width=&#8221;1/4&#8243; 
   offset=&#8221;vc_hidden-lg vc_hidden-md vc_hidden-sm vc_hidden-xs&#8221;]
   <style type=\"text/css\" scoped=\"scoped\">

我尝试使用Regex摆脱这些方括号[ ]之间的任何内容 这就是我写的:/\[[^]]*\]/g。 当我在regex website上运行时,此模式匹配,但是当我这样做时:

var exp = /\[[^]]*\]/g;
console.info('WP resp',latestReleaseNotes.content.rendered.search(exp));

它安慰了-1并且不匹配:

WP resp -1

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要转义其他]内的[],因为它会关闭表达式。

var exp = /\[[^\]]*\]/g

Working example