title: Preg_replace Regex That Ignores Enclosing Wordpress Shortcodes
body: <p>I'm building a PHP function that replaces "My Term" (for this example) in a user generated string with preg_replace, except when "My Term" is situated within enclosing shortcode tags, like so: </p>
  
  <pre><code>'Replace My Term here [link attr="ignore My Term here"]ignore My Term here[/link]'
  </code></pre>
  
  <p>So in essence, the regex should replace all instances of "My Term" in a string, unless they are between "[link" and "[/link]".</p>
  
  <p>I came up with the following regex, which matches "My Term" when it's not surrounded by alphanumerical characters and when it's not between square brackets.</p>
  
  <pre><code>preg_replace('~(?&lt;![A-Za-z0-9\]])My Term(?![A-Za-z0-9\[])(?=(?:(?:[^\]]*+\]){2})*+[^\]]*+\z)~', $replacement, $text);
  </code></pre>
  
  <p>However! This also ignores all instances of "My Term" between random shortcodes, like so:</p>
  
  <pre><code>'[shortcode1] this instance of My Term is ignored when it shouldnt be [shortcode2]'
  </code></pre>
  
  <p>How can I alter this regex to only ignore text when it is placed between enclosing "[link" and "[/link]" tags?</p>