Question

I need to be ECMA-262 regex compliant and NSRegularExpression is ICU regex compliant. According to Wikipedia, there are a few difference between the two. - are there any NSRegularExpression NSMatchingOptions or options I can pass to make NSRegularExpression ECMA-262 compliant (or at least closer to compliant)?

Code:

NSError * error;
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:regexString options:0 error:&error];
if (!error && [regex firstMatchInString:keyString options:0 range:NSMakeRange(0, keyString.length)]) {
    return YES;
}
Was it helpful?

Solution

The documentation of NSRegularExpression and the documentation on the syntax of ICU regular expression doesn't seem to contain anything that makes the engine compatible with ECMA-262 RegExp.

Going in the direction of using a JavaScript engine to evaluate a RegExp match compliant to ECMA-262 specs:

  • Load a UIWebView and make use of the method:

    - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
    

    It is said to be slow by the author of this question Embed a JavaScript engine in an iOS application

  • Use JavaScriptCore framework. Check JSContextRef.h, then JSBase.h to find out what you need to get it working.

    This answer from the same question also refers to videos of WWDC 2013 presentation, which introduces JavaScriptCore framework.

  • Also mentioned in another answer from the same question, you may build and ship a JavaScript engine with your app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top