Regular expressions: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
Added some external links related to regex. |
initial first paragraph; make see also scratch blocks use [] instead of () to specify text rather than numbers or a variable. |
||
Line 1: | Line 1: | ||
[[File:RegexpHighlight.webp|thumb|500px|alt=Lorem ipsum first paragraph where every consonant-vowel pair is highlighted|The alternating <mark style="background:rgb(255,255,0);color:rgb(0,0,0)">yellow</mark> and <mark style="background:rgb(255,221,136);color:rgb(0,0,0);">orange</mark> highlights show results for the following regexp pattern: <code>/[a-z](?<![aeiou])[aeiou]/gi</code> (any consonant-vowel pair)]] |
[[File:RegexpHighlight.webp|thumb|500px|alt=Lorem ipsum first paragraph where every consonant-vowel pair is highlighted|The alternating <mark style="background:rgb(255,255,0);color:rgb(0,0,0)">yellow</mark> and <mark style="background:rgb(255,221,136);color:rgb(0,0,0);">orange</mark> highlights show results for the following regexp pattern: <code>/[a-z](?<![aeiou])[aeiou]/gi</code> (any consonant-vowel pair)]] |
||
'''Regular expression''', often shorted to regex, is a |
'''Regular expression''', often shorted to '''regex''', is used to specify a [[w:Pattern matching|match pattern]] with just text. |
||
== Syntax == |
== Syntax == |
||
Line 52: | Line 52: | ||
== See also == |
== See also == |
||
* [[Match () with regex () ()|<sb>(Match |
* [[Match () with regex () ()|<sb>(Match [foo bar] with regex [foo] [g] :: operators)</sb>]] |
||
* [[Test regex () () with text ()|<sb><Test regex |
* [[Test regex () () with text ()|<sb><Test regex [foo bar] [g] with text [foo] :: sensing></sb>]] |
||
== External links == |
== External links == |
Revision as of 08:53, 2 July 2024
Regular expression, often shorted to regex, is used to specify a match pattern with just text.
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. |
See also
(Match [foo bar] with regex [foo] [g] :: operators)
<Test regex [foo bar] [g] with text [foo] :: sensing>
External links
- Regular expressions on Wikipedia
- TurboWarp extension gallery featuring TrueFantom's RegExp extension. It can be loaded into PenguinMod using
https://extensions.turbowarp.org/true-fantom/regexp.js
as the URL in the Load Custom Extensions popup. It adds more regex functionality into PenguinMod. - regex101, fairly useful little app with some fun challenges to test your knowledge of regex.
- MDN's Regular expressions documentation for JavaScript. There wasn't a good place to cite this, but I sourced at lot of stuff from here. Pretty much all of the names for each syntax element.