正则表达式

问题描述:

我打电话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\"> 

我尝试使用正则表达式来摆脱那些方括号[] 和这里就是我写之间的任何东西:/\[[^]]*\]/g

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

它安慰了-1和不匹配:

WP resp -1

我在做什么 这种模式,当我在regex website运行它,但是当我这样做匹配错误?

+0

您确定要使用正则表达式解析HTML标签? –

+0

@cᴏʟᴅsᴘᴇᴇᴅ我应该使用什么?我所瞄准的最终结果只是HTML而没有wordpress标签 –

+1

在regex101中使用正确的语言设置。看看[你的正则表达式](https://regex101.com/r/iCIvjy/1)与[正确的正则表达式](https://regex101.com/r/iCIvjy/2) –

您需要在其他[]内部跳过],因为否则它会关闭表达式。

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

Working example