Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between regular expressions in Java and ECMA-262 (AS, JS)

I need to convert Java regular expressions into Actionscript regular expressions.

There apparently aren't any premade converters, so I'm trying to write one myself. Is there any resource that'd list all the differences?

I'm aware of regular-expressions.info, but there doesn't seem to be a comprehensive listing of differences there.

Thanks

like image 413
tsiki Avatar asked Jun 22 '11 12:06

tsiki


2 Answers

I don't know of any existing converter, but if your target is specifically ActionScript and not just any ECMA-262 implementation, the job could be easier than you expected. AS3 is powered by PCRE, same as PHP's preg_ functions, so it supports lookbehind, atomic groups and possessive quantifiers, same as Java. It also supports Java's dotall and extended modes in addition to JS's ignore-case and multiline. It supports the inline modifier syntax ((?imsx)) as well.

PCRE's Unicode support is better than Java's, but unfortunately, I don't think that's included in ActionScript. The Unicode functionality seems to be explicitly tied to the character encoding, which is UTF-8 in PCRE; I believe ActionScript uses UTF-16 in accordance with ECMA-262. Anyway, its Unicode support seems to be minimal, same as JavaScript's.

like image 154
Alan Moore Avatar answered Sep 21 '22 11:09

Alan Moore


Here is a breakdown:

java.util.regex                                  RegExp.prototype
Character class set operations
(?#…) comment patterns
Named capture
The /y (sticky) modifier
The /x (extended) modifier
\u10000+ Codepoints
Possessive quantifiers
Quotation
Exception Handling
Linebreak matcher

References

  • ECMAScript RegExp bugs
  • What the JavaScript RegExp API Got Wrong, & How to Fix It
  • Non-Participating Groups: A Cross-Browser Mess
  • ECMAScript 3 Regular Expressions are Defective by Design
  • XRegExp: An Extended JavaScript Regex Constructor
  • Regex Recursion (Matching Nested Constructs)
  • Remove Nested Patterns with One Line of JavaScript
  • Validate Phone Numbers: A Detailed Guide
  • java.util.regex.Matcher
  • java.util.regex.Pattern
like image 28
Paul Sweatte Avatar answered Sep 22 '22 11:09

Paul Sweatte