tags:

views:

149

answers:

2

I have this:

var regExp:RegExp = new RegExp("((.*?)%)");

and want everything between the ( and the %) the string looks like this: (-24%)

I now get a return back "(-24" and have searched for a long time to find a solution but didn't find any.

+4  A: 

You need to escape the parentheses

use:

var regExp:RegExp = new RegExp("\\((.*?)%\\)");

for example, given the string "(-24%)", this regex will capture "-24"

edited my bad. forgot when creating the regex that way it needs double escape. fixed

Jonathan Fingland
still get this back traceRegExp: (-32%,(-32%,(-32I also tried different ways of executing the regExptext.match(regExp);regExp.exec(text);no difference
arno
The percent sign probably needs to be escaped too
Brian
Escaping characters doesn't mean it won't search for them. It just takes away their special meaning. For instance if this RegExp engine supports grouping you would want to escape the parens. If he's already returning a paren in his match than it's not an escaping issue.
Brian
new RegExp("\((.*?)\%\)"); still has the same (wrong) result i'm now trying the trim idea
arno
fixed the code above. should now work
Jonathan Fingland
thank you it is now workng fine
arno
A: 

You're probably going to have to do something with lookahead, lookbehind if you need to find what's inside something with a regexp.

Alternatively you could just trim the results after you get them.

Brian
Not in this case. All you have to do is match the part that interests you in a capturing group and retrieve it that way. Which is good, because JavaScript regexes don't support lookbehinds.
Alan Moore
Very cool. I know lookahead/lookbehind isn't a common feature. This is a neat little trick
Brian