RegExp
MediumIn plain terms
Regular expressions match patterns in text. Create with /pattern/flags or new RegExp(). Methods: test, exec. String methods: match, replace, search, split. Flags: g, i, m.
What you need to know
- •/pattern/flags
- •test, exec, match, replace
- •g global, i ignore case
Try it yourself
Copy the code below and run it in your browser console or a code editor:
const re = /\d+/g;
're 123 abc 456'.match(re); // ['123', '456']
/hello/i.test('Hello World'); // true (i = ignore case)
const email = /^[^@]+@[^@]+\.[^@]+$/;
email.test('a@b.com'); // true
'hello'.replace(/l/g, 'L'); // "heLLo"Learn more
Dive deeper with these trusted resources: