Please enable JavaScript.
Coggle requires JavaScript to display documents.
Regular Expression (Javascript) (uses (match, replace, search, split of…
Regular Expression
(Javascript)
is objects
uses
exec, test of ReGex
exec
A RegExp method that executes a search for a match in a string. It returns an array of information or null on a mismatch.
test
A RegExp method that tests for a match in a string. It returns true or false.
match, replace, search, split of String
match
A String method that executes a search for a match in a string. It returns an array of information or null on a mismatch.
search
A String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails.
replace
A String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.
split
A String method that uses a regular expression or a fixed string to break a string into an array of substrings.
Advanced searching with flags
g: global
i: case-insensitive
m: multi-line search
u: unicode
y: Perform a "sticky" search that matches starting at the current position in the target string
Creating
Regular Expression Literal
(compilation)
(better performance, constant)
Constructor of ReGex
Runtime Compilation
var re = new RegExp('ab+c');
use: regex is changing or unknown about
regex or from other resource(eg: user input)
Writing RegEx
Using Simple Pattern
(Exact match) /abc/
Special Character
\
precede Normal Char => not to be interpreted literally
precede Special Char => be interpreted literally
^
single line: beginning of line
multi line: immediately after new line break
diff meaning is first char in pattern set
$
match ending of line
multi line: immediately after new line break
*
match the preceding 0 or More times (={0,})
+
1 or More (={1,})
?
0 or 1 (={0,1})
use immediately after Quantifiers (*,+,?,{})
=> non-greedy (default: greedy)
eg: '123abc': /\d+/ => '123'; /\d+?/ => '1'
Lookahead Assertion x(?=y); x(?!y)
.
Single Char (except new line)
(x)
capturing parentheses
/(foo) (bar) \1 \2/
.replacement(/(...) (...)/, "$2 $1")
$& = whole matched string
(?:x)
match x but NOT remember
(non-capturing parentheses)
let you define subexpression
x(?=y)
Matches 'x' only if 'x' is followed by 'y'. This is called a lookahead.
/Jack(?=Sprat|Frost)/ (no keep Sprat or Frost)
x(?!y)
Matches 'x' only if 'x' is not followed by 'y'. This is called a negated lookahead.
/\d+(?!.)/.exec("3.141") matches '141' but not '3.141'
x|y
match x or y
{n}
Matches exactly n occurrences of the preceding expression. N must be a positive integer.
{n|m}
Where n and m are positive integers and n <= m. Matches at least n and at most m occurrences of the preceding expression. When m is omitted, it's treated as ∞.
[xyz]
Character set. Including escape sequences. Special characters the dot(.), asterisk (*) are not special inside a character set.
eg: /[a-z.]+/ and /[\w.]+/ match the entire string "test.i.ng"
[^xyz]
negative set
[\b]
Matches a backspace (U+0008). You need to use square brackets if you want to match a literal backspace character. (Not to be confused with \b.)
\b
Word boundary
length: 0
*note: "é" or "ü"
\B
Non-word boundary
\cX
X: A-Z
control-M
\d
= [0-9]
\D
non-digit character
\f
Matches a form feed (U+000C).
\ n
Matches a line feed (U+000A).
\r
Matches a carriage return (U+000D).
\s
Matches a single white space character, including space, tab, form feed, line feed. Equivalent to [ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff].
\S
other than spaces
\t
Matches a tab (U+0009).
\v
Matches a vertical tab (U+000B).
\w
Matches any alphanumeric character including the underscore. Equivalent to [A-Za-z0-9_].
\W
non-word
\n
Where n is a positive integer, a back reference to the last substring matching the n parenthetical in the regular expression (counting left parentheses).
\0
Matches a NULL (U+0000) character. Do not follow this with another digit, because \0<digits> is an octal escape sequence.
\xhh
Matches the character with the code hh (two hexadecimal digits)
\uhhhh
Matches the character with the code hhhh (four hexadecimal digits).
\u{hhhh}
(only when u flag is set) Matches the character with the Unicode value hhhh (hexadecimal digits).
Using Parentheses
default: remember
not -rem: (?:a)