JavaScripture
Contribute via GitHub Feedback

JavaScript 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. If flags contains 'g', this.global will be set to true. If flags contains 'i', this.ignoreCase will be set to true. If flags contains 'm', this.multiline will be set to true. RegExps can also be constructed using /pattern/flags syntax.

Example:

Run

Results:

 

Instance Properties

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.

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. Is automatically set to the index after a successful match. This property only applies if the RegExp is global.

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.

Example:

Run

Results:

 

source : String  

The pattern of this regular expression.

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.

Example:

Run

Results:

 

test(str : String) : Boolean

Returns true if this matches str.

Example:

Run

Results: