Regular expressions: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
Added all of the fun character classes. All of the unique escapes still need to be added, but I don't feel like it right now. |
oh, a nowiki was needed here |
||
Line 32: | Line 32: | ||
| <code>.</code> || Wildcard || Matches every character besides line terminators. Line terminators include <code>\n</code>, <code>\r</code>, <code>\u2028</code>, and <code>\u2029</code> || "Foo Bar" <code><nowiki>/./g</nowiki></code> -> <code>[ "F", "o", "o", " ", "B", "a", "r" ]</code> |
| <code>.</code> || Wildcard || Matches every character besides line terminators. Line terminators include <code>\n</code>, <code>\r</code>, <code>\u2028</code>, and <code>\u2029</code> || "Foo Bar" <code><nowiki>/./g</nowiki></code> -> <code>[ "F", "o", "o", " ", "B", "a", "r" ]</code> |
||
|- |
|- |
||
| <code>x|y</code> || Disjunction || Match something or something else. || "Foo Bar" <code><nowiki>/Foo|Bar/g</nowiki></code> -> <code>[ "Foo", "Bar" ]</code> |
| <code><nowiki>x|y</nowiki></code> || Disjunction || Match something or something else. || "Foo Bar" <code><nowiki>/Foo|Bar/g</nowiki></code> -> <code>[ "Foo", "Bar" ]</code> |
||
|- |
|- |
||
| <code>\</code> || Escape character || If a character is reserved for regex, such as <code><nowiki>*</nowiki></code>, <code><nowiki>|</nowiki></code>, or <code>.</code>. Note that this is itself a reserve character, so to match for it, you need to use <code>\\</code>. || "Foo.bar apple 78.9 banana" <code><nowiki>/[A-Za-z0-9]*\.[A-Za-z0-9]*/g</nowiki></code> -> <code>[ "Foo.bar", "78.9" ]</code> |
| <code>\</code> || Escape character || If a character is reserved for regex, such as <code><nowiki>*</nowiki></code>, <code><nowiki>|</nowiki></code>, or <code>.</code>. Note that this is itself a reserve character, so to match for it, you need to use <code>\\</code>. || "Foo.bar apple 78.9 banana" <code><nowiki>/[A-Za-z0-9]*\.[A-Za-z0-9]*/g</nowiki></code> -> <code>[ "Foo.bar", "78.9" ]</code> |
Revision as of 08:09, 2 July 2024
Regular expression, often shorted to regex, is a standard
Syntax
x
, y
, and z
when used under symbols are placeholders for text. Capital X
s, Y
s, and Z
s are used for number placeholders.
Symbol(s) | Name | Description | Example |
---|---|---|---|
Groups and backreferences | |||
(x) |
Capture group | Separates the content in the output. | "Foo Bar" /(Foo)|(Bar)/g -> [ "Foo", "Bar" ]
|
(?:x) |
Non-capture group | Acts as if the parentheses were not there | "Foo Bar" /(?:Foo)|(?:Bar)/g -> [ "Foo Bar" ]
|
(?<y>x) |
Named capture group | Equivalent to (x) , except it remembers the content used. |
"Foo Bar" /(?<F>Foo)|(?<B>Bar)/g -> [ "Foo", "Bar" ]
|
\k<y> |
Named backreference | References a previous named capture group, note that \k is literal |
"Foo Foo" /(?<Foo>Foo)\s\k<Foo>/g -> [ "Foo Foo" ]
|
Character classes | |||
[x-z] |
Character class | Matches every letter or number from x to z . |
"Foo Bar" /[a-f]/gi -> [ "F", "B", "a" ]
|
[xyz] |
References either x , y , or z |
"Foo Bar" /[FB]/g -> [ "F", "B" ]
| |
[^x-z] |
Negated character class | Matches every letter or number not from x to z . |
"Foo Bar" /[^a-f]/gi -> [ "o", "o", " ", "r" ]
|
[^xyz] |
References characters that aren't x , y , or z |
"Foo Bar" /[^FB]/g -> [ "o", "o", " ", "a", "r" ]
| |
. |
Wildcard | Matches every character besides line terminators. Line terminators include \n , \r , \u2028 , and \u2029 |
"Foo Bar" /./g -> [ "F", "o", "o", " ", "B", "a", "r" ]
|
x|y |
Disjunction | Match something or something else. | "Foo Bar" /Foo|Bar/g -> [ "Foo", "Bar" ]
|
\ |
Escape character | If a character is reserved for regex, such as * , | , or . . Note that this is itself a reserve character, so to match for it, you need to use \\ . |
"Foo.bar apple 78.9 banana" /[A-Za-z0-9]*\.[A-Za-z0-9]*/g -> [ "Foo.bar", "78.9" ]
|
Flags
Flag | Name | Description |
---|---|---|
g |
g lobal |
Search all of a string, rather than stopping once you find an occurrence. |