views:

1920

answers:

2

I'm hoping someone can see the problem in the following Regex replace example done using Flex:

remainder=inputString.replace(
     "\\("+bracketContents+"\\)(\\s)*(and|or)*(\\s|$)+",
     "");

For the following example:

Input: "a and (e or f)"

the normal regex expression given above would be:

\(e or f\)(\s)*(and|or)*((\s)+|$)+

which should leave a remainder string of "a and ", as I confirmed using http://gskinner.com/RegExr/ (Flex based) among others. It's all pretty standard stuff, so I hope it's not a Regex engine support issue. Thank you in advance.

EDIT: Including "/" in Flex String-based regex expression above doesn't change anything.

A: 

If I tried the following

var replacePattern:RegExp=new RegExp(
                   "\\("+bracketContents+"\\)(\\s)*(and|or)*(\\s|$)+",
                   "ig");

remainder=inputString.replace(replacePattern,"");

I get correct output. It'd be interesting if anyone knew why this happened.

raptors
A: 

This looks like a syntax issue. In your first post, you attempted to use the RegExp literal syntax, but this syntax should not be quoted and does not allow variables.

Flex thought you were trying to use a literal string as your replace pattern. Since the "bracketContents" variable is required, you have to use the RegExp constructor.

Had the variable not been required, the correct syntax for the RegExp literal would have been:

remainder=inputString.replace(/\(e or f\)(\s)*(and|or)*((\s)+|$)+/ig, "");
chrissr
Thank you for the clarification.
raptors