Links
- See also:
- XRegExp: A JavaScript regex
library that supports all native ES5 regular expression syntax, verbose mode, free scoping, etc.
- regexpu: Transpiles ES6 Unicode regular expressions to ES5.
- All MDN Articles
- Regular Expressions
- When you want to know whether a pattern is found in a string, use the test or search method; for more information (but slower execution) use the exec or match methods.
- RegExp
- regular-expressions.info
- Some limitations to be aware of:
- No named capturing groups :(
- No lookbehind (but you can lookahead.)
- Unicode support is limited to matching specific
codepoints via
\uFFFF
.
Flags
Flag |
Property |
Description |
g |
global |
Changes methods test() and exec() to start the search at lastIndex instead of the beginning of the string. Changes string.match to returns all matches and string.split to split at all matches. |
i |
ignoreCase |
ignore case. |
m |
multiline |
Changes ^ and $ to match at the beginning and end of a line. However, . still won't match across newlines. Use [\s\S] as an alternative. |
y |
(ES6) sticky |
Changes exec and test to match at lastIndex instead of conducting a search. Finally! |
Properties
Property |
Type |
RW |
Description |
lastIndex |
int |
RW |
specifies the index at which to start the next search. |
source |
string |
R |
the source text of the regexp object (without flags). |
global |
bool |
R |
true if the RegExp was constructed with the global ("g") flag. |
ignoreCase |
bool |
R |
true if the RegExp was constructed with the ignoreCase ("i") flag. |
multiline |
bool |
R |
true if the RegExp was constructed with the multiline ("m") flag. |
sticky |
bool |
R |
(ES6) true if the RegExp was constructed with the sticky ("y") flag. |
Methods
HowTo
Make .
match everything including newlines
There's no flag for this unfortunately. However you can
use the character set [\s\S]
instead to replace .
. So,
for example, /[\s\S]*/.test("abc\ndef\n123") == true
.