Alien Geography
Alien Geography

Reputation: 393

Pattern Substitution in Haxe

var str2 : String = "Expander Detected (%MSG_ID%)";
var r2 = ~/[\(%MSG_ID%\)]+/g;
trace(r2.replace(str2, ""));

Expected Result: Expander Detected

Actual Result: Expander etected

I need to replace (%MSG_ID%) in my strings. Characters before (%MSG_ID%) are dynamic, so we can not replace them manually.

Upvotes: 2

Views: 203

Answers (1)

Gama11
Gama11

Reputation: 34148

You need to remove the surrounding []. This works as expected:

var r2 = ~/\(%MSG_ID%\)+/g;

[] is a character set which matches if a single character contained in the set matches. Since the set happens to contain D, the D is also removed when calling replace(). However, you only want to match if all characters (and in that order) are present.

I'd recommend a tool like regex101.com for testing regexes. You can nicely see the issue there:

Upvotes: 6

Related Questions