Last active
March 19, 2019 22:19
-
-
Save bijenderk82/9c1e97c350585fdd8ad9a19332eb9b01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If you want to match strings that start with hey, use the ^ operator: | |
/^hey/.test('hey') //✅ | |
/^hey/.test('bla hey') //❌ | |
If you want to match strings that end with hey, use the $ operator: | |
/hey$/.test('hey') //✅ | |
/hey$/.test('bla hey') //✅ | |
/hey$/.test('hey you') //❌ | |
Combine those, and match strings that exactly match hey, and just that string: | |
/^hey$/.test('hey') //✅ | |
\d matches any digit, equivalent to [0-9] | |
\D matches any character that’s not a digit, equivalent to [^0-9] | |
\w matches any alphanumeric character, equivalent to [A-Za-z0-9] | |
\W matches any non-alphanumeric character, equivalent to [^A-Za-z0-9] | |
\s matches any whitespace character: spaces, tabs, newlines and Unicode spaces | |
\S matches any character that’s not a whitespace | |
\0 matches null | |
\n matches a newline character | |
\t matches a tab character | |
\uXXXX matches a unicode character with code XXXX (requires the u flag) | |
. matches any character that is not a newline char (e.g. \n) (unless you use the s flag, explained later on) | |
[^] matches any character, including newline characters. It’s useful on multiline strings. | |
? require zero or one item (optional) | |
/^\d?$/ | |
+ Match one or more (>=1) items | |
/^\d+$/.test('144343') //✅ | |
/^\d+$/.test('') //❌ | |
* Match 0 or more (>= 0) items | |
/^\d*$/.test('') //✅ | |
/^\d*$/.test('1a') //❌ | |
{n} Match exactly n items | |
/^\d{3}$/.test('123') //✅ | |
/^\d{3}$/.test('12') //❌ | |
/^\d{3}$/.test('1234') //❌ | |
/^[A-Za-z0-9]{3}$/.test('Abc') //✅ | |
{n,m} Match between n and m times: | |
/^\d{3,5}$/.test('123') //✅ | |
/^\d{3,5}$/.test('1234') //✅ | |
/^\d{3,5}$/.test('12345') //✅ | |
/^\d{3,5}$/.test('123456') //❌ | |
() create group | |
/^(\d{3})(\w+)$/.test('123') //❌ | |
/^(\d{3})(\w+)$/.test('123s') //✅ | |
()? optional group | |
/^(\d{3})(\s)?(\w+)$/.exec('123 s') | |
//Array [ "123 s", "123", " ", "s" ] | |
/^(\d{3})(\s)?(\w+)$/.exec('123s') | |
//Array [ "123s", "123", undefined, "s" ] | |
Named capturing groups | |
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/ | |
const result = re.exec('2015-01-02') | |
// result.groups.year === '2015'; | |
//result.groups.month === '01'; | |
// result.groups.day === '02'; | |
g: matches the pattern multiple times | |
i: makes the regex case insensitive | |
m: enables multiline mode. In this mode, ^ and $ match the start and end of the whole string. Without this, with multiline strings they match the beginning and end of each line. | |
u: enables support for unicode (introduced in ES6/ES2015) | |
s: (new in ES2018) short for single line, it causes the . to match new line characters as well. | |
\b matches a set of characters at the beginning or end of a word | |
\B matches a set of characters not at the beginning or end of a word | |
'I saw a bear'.match(/\bbear/) //Array ["bear"] | |
'I saw a beard'.match(/\bbear/) //Array ["bear"] | |
'I saw a beard'.match(/\bbear\b/) //null | |
'cool_bear'.match(/\bbear\b/) //null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment