JavaScripture
Contribute via GitHub Feedback

RegExp : Object

RegExp represents a regular expression that can be used for searching and extracting parts of Strings.

RegExp(pattern : String, [flags : String]) : RegExp

Constructors

new RegExp(pattern : String, [flags : String]) : RegExp

Constructs a new RegExp for the specified pattern. See flags for acceptable flag values. RegExps can also be constructed using /pattern/flags syntax.

Example:

Run

Results:

 

Instance Properties

dotAll : Boolean  

true if the RegExp was created with the 's' flag. DotAll RegExps will match any character for '.' including new lines ('\r' or '\n') which do not match '.' by default. Another option is to replace '.' with an empty inverted character class match '[^]' for systems that do not support ECMAScript 2018. See also multiline.

Example:

Run

Results:

 

flags : String  

Returns the flags specified when constructing this. Each character in flags represents a different option of the RegExp. 'g' is for global. 'i' is for ignoreCase. 'm' is for multiline. 's' is for dotAll. 'y' is for sticky.

Example:

Run

Results:

 

global : Boolean  

true if the RegExp was created with the 'g' flag. Global RegExps will match each place this.source occurs in the string, not just the first match. See also String.matchAll().

Example:

Run

Results:

 

ignoreCase : Boolean  

true if the RegExp was created with the 'i' flag. IgnoreCase RegExps will do case insensitive matching.

Example:

Run

Results:

 

lastIndex : Number

The starting index into the string for the next search. If this is sticky, the match must be found starting exactly at lastIndex. lastIndex is automatically set to the found index after a successful match. This property only applies if the RegExp is global or %%#sticky|sticky.

Example:

Run

Results:

 

multiline : Boolean  

true if the RegExp was created with the 'm' flag. Multiline RegExps will match '^' to the beginning of lines as well as the beginning of the string and match '$' to the end of lines as well as the end of the string. Note that multiline does not affect the '.' character class. See dotAll for more details.

Example:

Run

Results:

 

source : String  

The pattern of this regular expression.

Example:

Run

Results:

 

sticky : Boolean  

true if the RegExp was created with the 'y' flag. Sticky RegExps will only look for matches that start exactly at lastIndex. Non-sticky RegExps will try finding a match at all subsequent positions in the string if there's no match at lastIndex.

Example:

Run

Results:

 

Instance Methods

exec(str : String) : Array<String>

If this matches str, returns a new Array with item 0 equal to the portion of str that matched the regular expression, item 1 equal to the first capturing group in this, item 2 equal to the second capturing group in this, and so on. If this doesn't match str, returns null. See also String.match().

Example:

Run

Results:

 

You can name capture groups by placing ?<name> at the start of the group. The captured values are available on a groups property on the returned array. Note, this is new with ECMAScript 2018.

Example:

Run

Results:

 

If this is a global RegExp, exec can be called repeatedly to get all matches. Each time it is called, it updates this.lastIndex and begins the search from there. When there are no more matches, returns null. See also String.matchAll().

Example:

Run

Results:

 

test(str : String) : Boolean

Returns true if this matches str.

Example:

Run

Results: