Reputation: 545
How can I include a dynamic variable into a regexp object?
var pattern:RegExp = new RegExp(/'a '/ + keyword, '/gi');
In this case, I'd like the regex to match "a bird", or "A biRD" (if 'bird' is the keyword). However, 'bird' should not match if it is on its own.
Furthermore, I have a follow up question (I hope it's not too much to ask!): How would I do the exact thing above, but in which the regex only matches if the keyword is NOT preceded by 'a ' (I believe this involves the use of the ^ or the !, but I can't seem to find any clear documentation in any language for my specific request).
In the case of my second question test cases would look like this:
WORKS
abird
bird
bbbbirdddd
DOESN'T WORK
a bird
a birdddd
Any help will be greatly appreciated!
Upvotes: 4
Views: 1283
Reputation: 30273
ActionScript 3.0 supports lookbehinds and the case-insensitive modifier:
/(?<=a\s+)bird/i
See here for more information on how to construct a regular expression from a variable: Using a Variable in an AS3, Regexp.
For the second part of your question:
/(?<!a\s+)bird/i
Upvotes: 1