User:Steve0Greatness/drafts/Regular expression: Difference between revisions

From PenguinMod Wiki
Jump to navigation Jump to search
Content added Content deleted
m (Add whitespace)
(Added some external links related to regex.)
Line 54: Line 54:
* [[Match () with regex () ()|<sb>(Match (foo bar) with regex (foo) (g) :: operators)</sb>]]
* [[Match () with regex () ()|<sb>(Match (foo bar) with regex (foo) (g) :: operators)</sb>]]
* [[Test regex () () with text ()|<sb><Test regex (foo bar) (g) with text (foo) :: sensing></sb>]]
* [[Test regex () () with text ()|<sb><Test regex (foo bar) (g) with text (foo) :: sensing></sb>]]

== External links ==

* [[w:Regular expressions|Regular expressions]] on Wikipedia
* [https://extensions.turbowarp.org/ TurboWarp extension gallery] featuring [https://scratch.mit.edu/users/TrueFantom/ TrueFantom]'s RegExp extension. It can be loaded into PenguinMod using <code><nowiki>https://extensions.turbowarp.org/true-fantom/regexp.js</nowiki></code> as the URL in the ''Load Custom Extensions'' popup. It adds more regex functionality into PenguinMod.
* [https://regex101.com/ regex101], fairly useful little app with some fun challenges to test your knowledge of regex.
* [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions 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.

Revision as of 08:46, 2 July 2024

Lorem ipsum first paragraph where every consonant-vowel pair is highlighted
The alternating yellow and orange highlights show results for the following regexp pattern: /[a-z](?<![aeiou])[aeiou]/gi (any consonant-vowel pair)

Regular expression, often shorted to regex, is a standard

Syntax

x, y, and z when used under symbols are placeholders for text. Capital Xs, Ys, and Zs are used for number placeholders.


Syntax Reference
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 global Search all of a string, rather than stopping once you find an occurrence.

See also

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.