First problem is that your replacement value is the entire match, including the wildcards you wanted to remove.
(\b([\?|\*])\w+\b)
^ ^
\1 \2
There currently aren’t any capturing parentheses around the value you actually wanted to keep, the word \w+
characters.
Also, a word boundary \b
happens when you go from a word \w
character to a non-word \W
character. The question mark and asterisk are both non-word characters, so they may not trigger a word boundary at the beginning of what we colloquially would call a word.
You may also need to double up the backslashes. Remember that backslashes have special meaning in both regexes and in strings. In strings, for example, \t
represents a tab character, \b
represents a backspace character, and so on. If you want to represent a literal backslash, you usually have to do so as '\'.
All that being said, here are a couple approaches:
form.keyword = REreplace(form.keyword,'(\\s|^)[?*](\\w)','\1\2','all');
This matches whitespace or the beginning of the string, followed by either ? or * (they don’t have special meaning inside a character class), followed by word characters, and replaced by the whitespace and word characters we matched, leaving out only the characters you wanted to remove.
Here’s another option:
form.keyword = REreplace(form.keyword,'(?<=\\s|^)[?*](?=\\w)','','all');
In this option, the replacement is simpler. We technically only match the offending characters, so the replacement gets to be blank. But to do so we had to use some more advanced regex features: look-aheads and look-behinds.