Javascript Regex Flags

javascript-regex

// JavaScript - Regular Expression - Flags:

g: global match; find all matches rather than stopping after the first match
i: ignore case
m: multiline; treat beginning and end characters (^ and $) as working over 
   multiple lines (i.e., match the beginning or end of each line (delimited 
   by \n or \r), not only the very beginning or end of the whole input string)
u: unicode; treat pattern as a sequence of unicode code points
y: sticky; matches only from the index indicated by the lastIndex property of 
   this regular expression in the target string (and does not attempt to match 
   from any later indexes).

In JavaScript, there is no dot-match-all mode.  We can use something like:
[^]
[\s\S]
(.|[\r\n]) // Not recommended due to the capturing (results inslower performance)

We can specify a flag with a regular expression literal:

if (str.search(/abc/i) > -1) {
}

We can specify a flag with the RegExp regular expression by passing the second 
parameter:

var regexp = new RegExp(pattern[, flags]);
var results = new RegExp('[\\?&]' + name + '=([^&#]*)', 'i').exec(window.location.href);
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License