Les expressions régulières (regex)

Test & debug

https://regex101.com/

Méta-caractères

. ^ $ * + ? { } [ ] \ | ( )
  • \ utilisé pour annuler le statut de méta-caractère.
  • les méta-caractères ne sont pas actifs dans les classes ; par exemple, [ax\\+] représente l’ensemble ('a', 'x', '\', '+')
  *   match 0 ou plus du précédent              Ah* match "Ahhhhh" ou "A"

  ?   match 0 ou 1 du précédent                 Ah? match "A" ou "Ah"

  +   match 1 ou plus du précédent              Ah+ match "Ah" ou "Ahhh" mais pas "A"

  .   match n'importe quel caractère            do.* match "dog", "door", "dot", etc.

  []  match un ensemble de caractères           [cbf]ar match "car", "bar", ou "far"
                                                [0-9]+ match tout entier naturel
                                                [a-zA-Z] match une lettre ascii majuscule ou minuscule
                                                [^0-9] match tout caractère qui n'est pas un chiffre

  {}  match un nombre spécifique du précédent   [0-9]{3} match "315" mais pas "31"
                                                [0-9]{2,4} match "12", "123" et "1234"
                                                [0-9]{2,} match "1234567..."

  ^   dans une chaîne : début                   ^http match une chaîne qui commence par "http"

  ^   dans un [ ] : négation                    [^0-9] match tout caractère qui n'est pas un chiffre

  $   fin d'une chaîne                          The end$ match "The end"
  \d  un chiffre                                file_\d\d match "file_25"

  \w  un caractère de mot                       \w-\w\w\w match "A-b_1"

  \s  un espace (tab, space, newline)           a\sb\sc match "a b
                                                c"

  \D  un caractère qui n'est PAS un chiffre

  \W  un caractère qui n'est PAS un caractère de mot

  \S  un caractère qui n'est PAS un espace

Logique

  ()  capture un groupe                         A(nt|pple) match "Apple" (capture "pple")

  |   OU logique                                (Lun)|(Mar)di match "Lundi" ou "Mardi"

  \1  contenu du groupe 1                       r(\w)g\1x match "regex"

  \2  contenu du groupe 2                       (\d\d)\+(\d\d)=\2\+\1 match "12+65=65+12"

Flags

  a     Make \w, \W, \b, \B, \d, \D, \s and \S perform ASCII-only matching instead of full Unicode matching

  i     Perform case-insensitive matching; expressions like [A-Z] will also match lowercase letters.
        Full Unicode matching (such as Ü matching ü) also works unless the `a` flag is used to disable non-ASCII matches

  L     Make \w, \W, \b, \B and case-insensitive matching dependent on the current locale.
        The use of this flag is discouraged as the locale mechanism is very unreliable

  m     When specified, the pattern character '^' matches at the beginning of the string and at the beginning
        of each line (immediately following each newline); and the pattern character '$' matches at the end of
        the string and at the end of each line (immediately preceding each newline).
        By default, '^' matches only at the beginning of the string, and '$' only at the end of the string and
        immediately before the newline (if any) at the end of the string

  s     Make the '.' special character match any character at all, including a newline;
        without this flag, '.' will match anything except a newline

  u     Unicode matching

  x     Allows to write regular expressions that look nicer and are more readable by allowing to visually separate
        logical sections of the pattern and add comments. Whitespace within the pattern is ignored, except when
        in a character class, or when preceded by an unescaped backslash, or within tokens like *?, (?: or (?P<...>

Programmatiquement

JS

text = '14-07-1789'

regex = /\d+-\d+-\d+/
regex.test(text)
// affiche : true

text.match(regex)
// affiche : [ '14-07-1789', index: 0, input: '14-07-1789', groups: undefined ]

regex = /(?<dd>\d+)-(?<mm>\d+)-(?<yyyy>\d+)/
text.match(regex)
// affiche :
[
  '14-07-1789',
  '14',
  '07',
  '1789',
  index: 0,
  input: '14-07-1789',
  groups: { dd: '14', mm: '07', yyyy: '1789' }
]

regex = new RegExp('(?<dd>\\d+)-(?<mm>\\d+)-(?<yyyy>\\d+)')
// noter l'escape des caractères spéciaux
text.match(regex)

Examples

email

([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})

url

(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?

Adresse IP

(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

Code postal français

((0[1-9])|([1-8][0-9])|(9[0-8])|(2A)|(2B))[0-9]{3}