简体   繁体   中英

RegEx for processing WordPress shortcode

I am trying to find a suitable regex for processing WordPress shortcodes. Created an expression but it doesn't process all conditions.

Expression

\\[feature([^\\]]*)\\]([^\\]]*)\\[\\/feature\\]

Example text

[feature title="Call us at" width="4" icon="fa-thumbs-up"]my phone is[/feature]

// shortcodes without linebrakes between
[feature title="Call us at" width="4" icon="fa-thumbs-up"]my phone is[/feature][feature title="Call us at" width="4" icon="fa-thumbs-up"]my phone is[/feature]


// with nested shortcode inside attributes


[feature title="Call us at [phone]" width="4" icon="fa-thumbs-up"]my phone is [other][/feature]


[feature title="Call us at [phone] sfdfasd" width="4" icon="fa-thumbs-up"]my phone is [other] dssafsd[/feature]

First two example works but for the nested shortcodes regex fails.

Here is the link for you to play with https://regex101.com/r/zA4iH4/7

Specifically if you check the documentation for shortcodes you will find Attribute values must never contain the following characters:

  • Square braces: [ ]
  • Quotes: " '

So your sample text above is not strictly valid for WordPress and you will likely have other issues.


As long as you don't have nested [feature...] tags (where a [feature] is inside a [feature] ) and no square braces in attributes:

Regular Expression

\[feature([^\]]*)\]([\s\S]*?)\[\/feature\]

Human Readable

// \[feature([^\]]*)\]([\s\S]*?)\[\/feature\]
// 
// Options: Case insensitive; ^$ don’t match at line breaks
// 
// Match the character “[” literally «\[»
// Match the character string “feature” literally (case insensitive) «feature»
// Match the regex below and capture its match into backreference number 1 «([^\]]*)»
//    Match any character that is NOT a “]” «[^\]]*»
//       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
// Match the character “]” literally «\]»
// Match the regex below and capture its match into backreference number 2 «([\s\S]*?)»
//    Match a single character present in the list below «[\s\S]*?»
//       Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
//       A “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed) «\s»
//       Any character that is NOT a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed) «\S»
// Match the character “[” literally «\[»
// Match the character “/” literally «\/»
// Match the character string “feature” literally (case insensitive) «feature»
// Match the character “]” literally «\]»

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM