Regular Expressions
What are Regular Expressions? #
- A type of formal language (pattern) that can be used to search and replace strings.
- From simple character searches to complex character matching functions like email and password validation, regular expression patterns can perform these tasks quickly.
Role of Regular Expressions #
- Character Search
- Character Replacement
- Character Extraction
Test Site #
Creating JS Regular Expressions #
Constructor Function Method #
You can use it by calling the RegExp constructor function.
const regexp1 = new RegExp("abc");
// new RegExp(pattern)
const regexp2 = new RegExp("^abc","gi");
// new RegExp(pattern , flags)
Literal Method #
Regular expressions use patterns enclosed by / as literals.
const regexp1 = /^abc/;
// /pattern/
const regexp2 = /^abc/gi;
// /pattern/flags
gi : Case-insensitive
Regular Expression Methods #
| Method | Syntax | Description |
|---|---|---|
| test | regex.test(string) | Match status (Boolean) |
| match | string.match(regex) | Returns an array of matching characters |
| replace | string.replace(regex, replacement) | Replaces matching characters |
let str = `
010-1234-5678
s22043@gsm.hs.kr
The quick brown fox jumps over the lazy dog.
abbcccdddd
`
const regexp = /fox/gi
console.log(regexp.test(str)) // true
console.log(str.replace(regexp,'AAA')) // fox >> AAA
Flags (Options) #
| Flag | Description |
|---|---|
| g | Global match |
| i | Case-insensitive match (English) |
| m | Multiline match |
g, gi
let str = `
010-1234-5678
s22043@gsm.hs.kr
The quick brown fox jumps over the lazy dog.
abbcccdddd
the apple
`
const regexp = /the/
console.log(str.match(regexp)) // Matches only one word containing lowercase 'the'
const regexp2 =/the/g
console.log(str.match(regexp)) // Outputs all lowercase 'the'
const regexp3 = /the/gi
console.log(str.match(regexp)) // Outputs all 'the', including uppercase 'The'
m
let str = `
010-1234-5678
s22043@gsm.hs.kr
The quick brown fox jumps over the lazy dog.
abbcccdddd
the apple
`
console.log(str.match(/\.$/gim)) // ["."]
$Returns only the first sentence that matches the regular expression.
\(Escape character) refers to a character whose original function changes when preceded by a backslash.
Patterns (Expressions) #
| Pattern | Description |
|---|---|
| ^ab | Matches 'ab' at the start of a line |
| ab$ | Matches 'ab' at the end of a line |
| . | Matches any single character |
| a | b |
| ab? | Matches 'ab' or 'a' (b is optional) |
| {3} | Matches exactly 3 consecutive occurrences |
| {3 ,} | Matches 3 or more consecutive occurrences |
| {3 , 5} | Matches 3 to 5 consecutive occurrences |
let str = `
010-1234-5678
s22043@gsm.hs.kr
The quick brown fox jumps over the lazy dog.
abbcccdddd
the apple
d
https://local.1234
http://local.12345`
console.log(
str.match(/d$/g)
) // ["d"]
console.log(
str.match(/d$/gm)
) // ["d"] ["d"]
console.log(
str.match(/^t/gim)
) // ["t"] , ["T"]
console.log(
str.match(/./g) // An array of all characters is returned.
)
console.log(
str.match(/.a..e/g)
) // ["apple"]
console.log(
str.match(/fox|dog/g)
) // ["fox"] , ["dog"] if 'g' is absent, only 'fox' is output
console.log(
str.match(/https?/g)
) // ["https"] ,["http"]
console.log(
str.match(/d{2}/g)
) // dd dd << is an array
console.log(
str.match(/\b\w{2,3}\b/g)
// Outputs only words consisting of 2 to 3 characters, regardless of English letters or numbers
)
| Pattern | Description |
|---|---|
| [abc] | a or b or c |
| [a-z] | Matches characters in the range from a to z (lowercase English letters) |
| [A-Z] | Matches characters in the range from A to Z (uppercase English letters) |
| [0-9] | Matches characters in the range from 0 to 9 (numbers) |
| [가-힣] | Matches characters in the range from '가' to '힣' (Korean characters) |
| \w | Matches 63 characters (Word, 52 uppercase/lowercase English letters + 10 numbers + _) |
| \b | Word boundary (matches positions that are not part of a 63-character word) |
| \d | Matches a digit |
| \s | Matches whitespace |
| (?=) | Positive lookahead |
| (?<=) | Positive lookbehind |
let str = `
010-1234-5678
s22043@gsm.hs.kr
The quick brown fox jumps over the lazy dog.
abbcccdddd
the apple
d
https://local.1234
http://local.12345`
const h = ` the hello world ! `
console.log(
str.match(/[fox]/g) // Both 'f' and 'o' are found
)
console.log(
str.match(/[0-9]{1,}/g) // All 0-9 characters, one or more, are found
)
console.log(
str.match(/\w/g) // All English characters are output
)
console.log(
str.match(/\bf\w{1,}/g) // Breaks at a boundary that is not f ~ 63 characters. ["fox"]
)
console.log(
h.replace(/\s/g,'') // Can be applied to delete whitespace characters
)
Positive Lookahead, Positive Lookbehind
let str = `
010-1234-5678
s22043@gsm.hs.kr
The quick brown fox jumps over the lazy dog.
abbcccdddd
the apple
d
https://local.1234
http://local.12345`
console.log(
str.match(/.{1,}(?=@)/g) // Array of one or more characters before '@' ["s22043"]
)
console.log(
str.match(/(?<=@).{1,}/g) // ["gmail.com"]
)