Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
gs

Test String

Code Generator

Generated Code

const regex = /(^|(?<=\n))([0-2]\d{1}:[0-5]\d{1}:[0-5]\d{1}\.\d{3}) --> ([0-2]\d{1}:[0-5]\d{1}:[0-5]\d{1}\.\d{3})(.*?)((?=[0-2]\d{1}:[0-5]\d{1}:[0-5]\d{1}\.\d{3})|$)/gs; // Alternative syntax using RegExp constructor // const regex = new RegExp('(^|(?<=\\n))([0-2]\\d{1}:[0-5]\\d{1}:[0-5]\\d{1}\\.\\d{3}) --> ([0-2]\\d{1}:[0-5]\\d{1}:[0-5]\\d{1}\\.\\d{3})(.*?)((?=[0-2]\\d{1}:[0-5]\\d{1}:[0-5]\\d{1}\\.\\d{3})|$)', 'gs') const str = `00:00:34.355 --> 00:00:36.635 Бублики! Купите горячии бублики! 00:00:37.315 --> 00:00:40.315 Ты что сделал, подлец?! Все ботинки мне изгадил! 00:00:40.995 --> 00:00:42.155 Пошел вон! 00:00:42.275 --> 00:00:46.875 Ваше благородие, да бросьте вы этого дурака! Идите до сюда, зараз все исправим.Давайте. 00:00:48.275 --> 00:00:49.715 На, чисть быстро. 00:00:49.915 --> 00:00:57.715 Один момент. Такие сапоги перемазали, мама родная! У меня вакса-то германская, фирма "Лавалин". 00:00:57.915 --> 00:01:00.755 Не вакса, а масло просто, хоть на хлеб мажь. 00:01:01.795 --> 00:01:02.875 Чисть, чисть. 00:01:04.195 --> 00:01:05.315 Ой. Ваше благородие 00:01:05.875 --> 00:01:06.955 Что с тобой? 00:01:07.475 --> 00:01:08.875 Живот прихватило… Не могу.. 00:01:09.035 --> 00:01:10.355 Ты куда? Иди сюда 00:01:10.635 --> 00:01:12.035 По нужде, ваше благородие! 00:01:14.915 --> 00:01:18.355 А помнишь погром на Дальницкой? Дракон чертов 00:01:30.595 --> 00:01:33.035 Или это я не знаю какая бывает жизнь? 00:01:36.915 --> 00:01:39.955 Нас родила мама. Так же как и вас 00:01:41.435 --> 00:01:46.195 Эта женщина - наша мама - хотела чтоб мы жили, 00:01:46.235 --> 00:01:48.035 а не мучались 00:01:48.395 --> 00:01:50.955 Она хотела чтоб мы жили хорошо 00:01:51.835 --> 00:01:55.515 и она была права, как может быть права только наша мама... 00:01:57.275 --> 00:02:00.555 А Вы говорите, я не знаю, какая бывает жизнь... 00:02:16.755 --> 00:02:18.395 Стой! Стоп! `; // Reset `lastIndex` if this regex is defined globally // regex.lastIndex = 0; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions