Regular expression

The RegExp class represents a regular expression in JavaScript.

Text between slashes /.../is literal representation of a regular expression. After the second slash, flags may follow, for example /\d+/g.

The String class also has methods that works with regex.

You can escape special characters by backslash \ for using them as regular characters. And obviously, you must escape backslash character when you create regex from string as \\.

Result of matching is array, where first item is full match, and others correspond to the capture groups. The matchAll method of String returns an iterable object over all matches.

Follow link to read common regular expression syntax.

var txt = "//single line comment\r\nint x=23; \r\n" +
          "/* line 1 \r\n line 2 \r\n */" +
          "void tst(){}";

// literal regex to match comments
var regexp1 = /(\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/)|(\/\/.*)/g;

var regexp2 = // create regex via new operator
    new RegExp(/(\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/)|(\/\/.*)/, "g");

var regexp2a = // create regexp via factory method
     RegExp(/(\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/)|(\/\/.*)/, "g");

// str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); 
var regexp3 = // create regex via new operator from string
    new RegExp("(\\/\\\*\(\[\^\*\]\|\[\\r\\n\]\|\(\\\*\+\(\[\^\*/\]\|"
        +"\[\\r\\n\]\)\)\)\*\\\*\+\\/\)\|\(\\/\\/\.\*\)", "g");   



constructor description
RegExp(pattern[, flags]) Returns new regex. It can be used with new operator or as factory method.

The pattern parameter is text of regex or other regex object.

property description
lastIndex The index at which to start the next match. Used with flag g or y (sticky).
flags A string that contains the flags of this regex.
dotAll Defines whether . matches newlines or not.
global Defines whether to test the regular expression against all possible matches in a string, or only against the first.
ignoreCase Defines whether to ignore case while attempting a match in a string.
multiline Defines whether or not to search in strings across multiple lines.
source The text of the pattern.
sticky Defines whether or not the search is sticky, i.e. searches in strings only from the index indicated by the lastIndex property of this regular expression.
unicode Defines whether or not Unicode features are enabled.
method description

Executes a search for a match in a specified string. Returns a result array, or null.

If the regex has the flag g or y then a lastIndex will be saved from previous match.

test(str) Returns true if there is a match between the regular expression and the given string.
toString() Returns a string representing the regular expression.

string methods

Below methods of String that work with regex.

method description
match(regexp) Retrieves the result of matching a string against a regular expression. Returns null if no matches are found, or Array with following content:
  • If the g flag is used, all results matching the complete regular expression will be returned, but capturing groups will not.
  • If the g flag is not used, only the first complete match and its related capturing groups are returned. In this case, the returned item will have additional properties as described below.
matchAll(regexp) Returns an iterator of all results matching a string against a regular expression, including capturing groups. The regexp parameter must have the g flag, otherwise a TypeError will be thrown.
search(regexp) Searches first match between the regular expression and the given string. Returns position of match, or -1 if nothing found.
replace(regexp|substr, newSubstr|func)

Returns a new string with some or all matches of a pattern replaced with a replacement string specified by the newSubstr or generated by the func function.

If pattern is a string, only the first occurrence will be replaced.

The newSubstr parameter can include the following special replacement patterns:

  • $$ - inserts $
  • $& - inserts the matched substring
  • $` - inserts the portion of the string that precedes the matched substring
  • $' - inserts the portion of the string that follows the matched substring
  • $n - inserts the nth parenthesized submatch string, i.e. capture group
  • $<name> - inserts the named capture group (be careful, not all browsers support this)
replaceAll(regexp|substr, newSubstr|func) Returns a new string with all matches of a pattern replaced by a replacement.

If the replacement string specified by regular expression, it must have flag g.

split([delim[, limit]]) Splits string into an array of strings. Parameters:
  • delim - string or a regular expression that used as delimiter.
  • limit - maximum size of resulting array.