 |
Free EcmaScript Interpreter.
A JavaScript interpreter written
in Java. |
Language extensions - Regular
Expressions |
There regular expression can either user the GNU RegExp package or the
ORO Inc package. The ORO inc package is used if the package com.oroinc.text.regex
is on the class path, otherwise the GNU package is used (the GNU classes
are part of the distributed binary).
You can evaluate the value of RegExp to find which version is used (it
prints the name of the class of the RegExp extension loaded).
The RegExp extension provides a regular expression bsed search
and replacement mechanism which is similar to the same capabilities in
latest version of JavaScript by Netscape. It is a subset of these capabilities,
as the Netscape variant add incompatible extensions to the EcmaScript syntax,
and could not be extended to support multithreading.
The ORO regular expression extension uses the OROMatcher
package and therefore depends on it for the detailed capabilities of the
matcher, which is highly compatible with Perl 5. Details on the syntax
and semantic of regular expressions can be found in the ORO
documentation. Version 1.1 is used.
The GNU regular expression
extension allow to distribute programs which are fully LGPL. Only the classes
are included in the FESI distribution itself, the original sources being
available at gnu.regexp-1.0.8.tar.gz.
The version 1.0.8 is used.
Regular expressions are built by the RegExp constructor and
can then be used either by RegExp specific functions or selected
String
functions.
OptionalRegExp extension
If you do not know which regular expression matcher will be available at
run-time, but you would like to provide it to user if available, then your
main program can load Fesi.Extensions.OptionalRegExp instead of
specifically Fesi.Extension.ORORegExp or Fesi.Extension.GNURegExp.
That way there will be no error message at load time, only at execution
time of a script attempt to create a RegExp object. This is the
method used by the interpreter bundled with the package.
RegExp constructor
The RegExp constructor creates a regular expression from a text
representation. The regular expression is created as case sensitive and
for single replacement. Its syntax is not checked before its first use.
Exemple:
r = new RegExp("[a-z][a-z0-9]*");
It is not possible to create regular expressions by using the /regexp/
syntax.
RegExp objects
The objects created by the RegExp constructor keep some attributes
of the regular expression, as its case insensitiveness and its substitution
mode. The recognized attributes are:
-
global
-
Use global (multiple) replacement in the replace function if true.
false
by default.
-
ignoreCase
-
Ignore case if true. false by default.
The attributes can be changed anytime. The regular expression is automatically
recompiled if neeeded. There is therefore no compile routine.
The following routines are defined on regular expression objects:
-
exec("string")
-
Does a match on the string with the regular expression. Return null
or a result array (see below).
-
test("string")
-
Does a match on the string with the regular expression, returning true
or false (faster than test)..
Added String routines
The following String routines are added or enhanced:
-
match(regexp)
-
Does a match on the string with the regular expression. Return null
or a result array (see below).
-
replace(regexp,replacementString)
-
Replaces the first (default) or all (if global is true)
occurences of the regular expression with the replacement String, extrapolation
$n variables.
-
search(regexp)
-
Does a match on the string with the regular expression. Returns -1 if not
found or the index of the start of the match if found. Faster than match.
-
split(regexp[,limit])
-
Split the string at regexp points, returning at most limit (defaults to
all) substrings. The default split behaviour is still used if
the parameter is not a regexp.
Result array
A result array is returned by some routines. It includes both properties
and the strings as the array elements. The properties are::
-
index
-
The index of the start of the match.
-
input
-
The string which was matched.
The array elements are the total match followed by the submatches
(the parts inside parentheses).
If there is no match, a null (which is tested as false)
is returned.
Return to the main page
Last update: 29 July 2000