Here is my program:
<cfset test = 'a~b~~c~d~~~e'>
<cfset test2 = Replace(test, '~~','~X~','all')>
<cfoutput>
test #test#
<br> test2 #test2#
<br>wanted: a~b~X~c~d~X~X~e
</cfoutput>
The output I got:
test a~b~~c~d~~~e
test2 a~b~X~c~d~X~~e
wanted: a~b~X~c~d~X~X~e
So the output of test2 is wrong This no doubt has to do with the inner workings of the Replace function, but I need it to work correctly.
Does anyone know of a workaround for this problem?
It's not a bug.
Replace() doesn't have any special "lookaround" capability. It just walks the input string until it finds ~~
. Then jumps to the next character - after the matched text - and continues searching. Resulting in only two matches.
It sounds more like the requirement is to insert an "X" in between any two tildes "~~". A regex with a non-capturing look-ahead should accomplish that.
reReplace(test, '~(?=~)','~X','all')
Explanation
~
Find tilde(?=~)
.. followed by another tilde