#include <MsgBoxConstants.au3> ; to declare the Constants of MsgBox
Local $sRegex = "(?m)\bFOO\b(?![^[\]]*(?:\[\[[^[\]]*]][^[\]\n]*)*]])"
Local $sString = "Should match the following" & @CRLF & _
"lorem ipsum FOO dolor" & @CRLF & _
"lorem ipsum FOO dolor [[amet]]" & @CRLF & _
"[[lorem]] ipsum FOO dolor [[sit]] amet" & @CRLF & _
"" & @CRLF & _
"Should NOT match the following" & @CRLF & _
"lorem ipsum [[FOO]] dolor" & @CRLF & _
"lorem [[ipsum FOO dolor]] sit amet" & @CRLF & _
"lorem [[ipsumFOOsolor]] sit amet" & @CRLF & _
"[[lorem]]" & @CRLF & _
"[[ipsum-FOO&dolor-sit.pdf#page=130]]" & @CRLF & _
"lorem ipsumFOOsolor sit amet" & @CRLF & _
"" & @CRLF & _
"FOO should be matched below but is not because the double-brackets are not balanced." & @CRLF & _
"lorem FOO [[ipsumFOOsolor]] sit ]] amet"
Local $sSubst = "BAZ"
Local $sResult = StringRegExpReplace($sString, $sRegex, $sSubst)
MsgBox($MB_SYSTEMMODAL, "Result", $sResult)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm