Regex for “Token” Replacement
I think this is the Regex statement I use the most, and yet every single time I end up writing it 3 different ways before getting it correct. It came in handy again when I was writing my post about Keyboard Shortcuts, as I created a shorthand to be replaced by a larger html snippet later for formatting.
In my post content (before publishing) I had placeholders like:
- Go to the global menu, select Edit, then select Paste
- Cost: [[Mouse Movement]] {{1}}, [[Click]] {{1}}, [[Mouse Movement]] {{1}}, [[Click]] {{1}}
- Right-click, Select Paste
- Cost: [[Click]] {{1}}, [[Click]] {{1}}
- Potential add of [[Device Movement]] {{4}} if not using Copy Method 1 (mouse) prior
That I wanted to end up with WordPress short codes like:
- Go to the global menu, select Edit, then select Paste
- Cost: [ item_and_value item="Mouse Movement" val="1"][/item_and_value], [ item_and_value item="Click" val="1"][/item_and_value], [ item_and_value item="Mouse Movement" val="1"][/item_and_value], [ item_and_value item="Click" val="1"][/item_and_value]
- Method 1: Mouse Shortcut
- Right-click, Select Paste
- Cost: [ item_and_value item="Click" val="1"][/item_and_value], [ item_and_value item="Click" val="1"][/item_and_value]
- Potential add of [ item_and_value item="Device Movement" val="4"][/item_and_value] if not using Copy Method 1 (mouse) prior
To do this I used the following Regex: http://regexr.com/3gnv6
Be sure to use Regexr’s great hover tips for explaination of any part of the statement.The core bit is:
\[\[(.*?)\]\]
This says save anything you find between [[ and ]] as a “Capture Group”, or a value to be used in a Replace function after Matching. The most common mistake I make every time is using:
(.*) or (.+)
… which is “greedy” and always goes until the last reference of ]]. So if you have multiple in a line it treats the WHOLE set as the ONE value. The trick is instead to use:
(.*?)
… which is “non-greedy” and just get the first instance. So simple, yet I forget it just about every time.