Regex to replace a word that is not enclosed between tags (non-html)

Limited regex experience, I'm working in PHP with preg_replace.I want to replace a specified "word" that is NOT between the [no-glossary] ... [/no-glossary] tags. My expression works if their are not spaces between the "word" and tags, or if their is a space after the "word", but if I put a space (exepected) before the word it fails!These work:\[code\]$html = '<p>Do not replace [no-glossary]this[/no-glossary] replace this.</p>';$html = '<p>Do not replace [no-glossary]this [/no-glossary] replace this.</p>';\[/code\]This doesn't:\[code\]$html = '<p>Do not replace [no-glossary] this [/no-glossary] replace this.</p>';\[/code\]Pattern used explained part-by-part\[code\]/ - find(?<!\[no-glossary\]) - Not after the [no-glossary] tag[ ]* - Followed by 0 or more spaces (I think this is the problem)\b(this)\b - The word "this" between word boundaries[ ]* - Followed by 0 or more spaces(?!\[\/no-glossary\]) - Not before the [/no-glossary] tag/\[/code\]Here is the code:\[code\]$pattern = "/(?<!\[no-glossary\])[ ]*\b(this)\b[ ]*(?!\[\/no-glossary\])/"; $html = '<p>Do not replace [no-glossary] this [/no-glossary] replace this.</p>';$html = preg_replace($pattern, "that", $html);print $html;\[/code\]Output:\[code\]<p>Do not change [no-glossary] that [/no-glossary] changethat.</p>\[/code\]Problems:[*]word was changed between tags.[*]space removed in front of the second word that was correctly replaced.
 
Back
Top