Javascript Regex
.
means "any character".*
means "any number of this".^
says at the beginning of the string.$
says at the end of the string.
.* Zero or more of any characters. This can be used in the middle anywhere.
Example: \(joseph)\s.*(stalin)\
the .* in the above line will match any middle name that may come between joseph and stalin.
^.* Start of string followed by zero or more of any character (except line break)
it will match anything it tested on it's own. But you'll probably never use it standalone.
.*$ //Zero or more of any character (except line break) followed by end of string
(
and )
denote capture groups, meaning you can search for repeating subgroups in a string using these. You can put regex of the pattern that will repeat in between these. (a-z0-9)
will match the exact string "a-z0-9"
and allows two additional things: You can apply modifiers like *
and ?
and +
to the whole group, and you can reference this match after the match with $1
or \1
. Not useful with your example, though. Here for example we are replacing big bang with $1 and $2 using capture groups.
"big bang".replace(/(\w+)\s(\w+)/, '$2 $1');
\w
The \w metacharacter is used to find a word character.
A word character is a character from a-z, A-Z, 0–9, including the _ (underscore) character.
var str = “Give 100%!”;
var patt1 = /\w/g;
var result = str.match(patt1);
this will give individual characters: ‘G,i,v,e,1,0,0’