0

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

2
  • In WordPress you don't parse the shortcodes yourself use the Shortcode API and understand that there are limitations with nested shortcodes Commented Dec 21, 2015 at 10:54
  • Sorry I didn't mention. I am using JS not PHP. Commented Dec 21, 2015 at 11:04

1 Answer 1

1

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 «\]»
Sign up to request clarification or add additional context in comments.

6 Comments

It is not working when there is any nested shortcode inside attributes.
Added support matched pair double quotes used for parameters, although it should be noted that WordPress explicitly doesn't support use of shortcodes in parameter / attribute values.
Specifically if you check the documentation for shortcodes you will find Attribute values must never contain the following characters: Square braces: [ ] and Quotes: " '
Updated answer to remove updated regular expression as it would not be strictly valid for WordPress.
yes, you are correct. Putting shortcodes inside attribute values is bad practice until WP core supports it! Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.