1

I would like to dynamically create the Regular Expression in the code below, by working out the permutations I need to detect for, then creating the Regular Expression like concatenating a string, but no matter what I try it fails.

I've found that if you trace testRX.source, you can see the regexp without escape characters ( thats what I believe anyway ).

Ideally I would like to do this

var permutationsString:String = "abc|acb|bac|bca|cab|cba";
var regex1:RegExp = "/\b("+permutationsString+")\b/g";
regex1.test( "whatever" );

but it does not work.

Here's working code below

var testRX:RegExp = /\b(abc|acb|bac|bca|cab|cba)\b/g;

var regex:RegExp = new RegExp( testRX );

trace( "regex.test = " + regex.test( "333" )); // false

trace( "regex.test = " + regex.test( "abc" )); // true

trace( "regex.test = " + regex.test( "ca"  )); // false

trace( "regex.test = " + regex.test( "bbb" )); // false

trace( "regex.test = " + regex.test( "abce")); // false

This is Actionscript 3.

Thanks in advance.

gingerman

0

1 Answer 1

2

You need to remove the first forward slash (as that is just for starting a RexEx literal which you aren't doing now that you're building a string), escape the backslashes (since you're creating a string literal), and move the regex flags into the second parameter of the RexEx constructor:

var regex1:RegExp = new RegExp("\\b(" + permutationsString + ")\\b","/g");

This should then do what you'd like.

Sign up to request clarification or add additional context in comments.

1 Comment

Batman your reputation is well deserved. Thanks that works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.