ReasonJun

javascript : regexp pattern 본문

Frontend/Javasciprt

javascript : regexp pattern

ReasonJun 2023. 6. 9. 00:42
728x90

The code you provided demonstrates the usage of regular expressions in JavaScript to match and manipulate text based on specific patterns. Let's go through the code and understand its functionality:

const str = `...`; // The text content to be matched and manipulated

console.log(str.match(/^h.../gm)); // Matches strings starting with 'h' followed by any three characters.

console.log(str.match(/\\.com/g)); // Matches '.com' occurrences in the text.
console.log(str.match(/\\.com$/g)); // Matches '.com' only at the end of lines.
console.log(str.match(/\\.com$/gm)); // Matches '.com' at the end of lines.
console.log(str.match(/...\\.com$/gm)); // Matches three characters followed by '.com' at the end of lines.

console.log(str.match(/fox|dog/g)); // Matches 'fox' or 'dog' occurrences.
console.log(str.match(/fox|dog|\\.com/g)); // Matches 'fox', 'dog', or '.com' occurrences.

console.log(str.match(/https?/g)); // Matches 'http' or 'https'.

console.log(str.match(/\\d{3}/g)); // Matches three consecutive digits.
console.log(str.match(/d{3}/g)); // Matches 'ddd'.
console.log(str.match(/\\d{3,9}/g)); // Matches a sequence of digits between 3 and 9 characters long.

console.log(str.match(/[ab]/g)); // Matches 'a' or 'b' occurrences.
console.log(str.match(/[a-z]{1,}/g)); // Matches lowercase words.
console.log(str.match(/[a-zA-Z]+/g)); // Matches words regardless of case.

console.log(str.match(/\\b[0-9]+\\b/g)); // Matches whole numbers.
console.log(str.match(/\\s/g)); // Matches whitespace characters.

console.log(str.match(/https?:\\/\\/\\w+\\.?/g)); // Matches URLs starting with 'http://' or 'https://'.
console.log(str.match(/https?:\\/\\/(?:\\w+\\.?)+\\/?/g)); // Matches complete URLs.

console.log(str.match(/.+(?=과)/g)); // Matches text before '과' using a positive lookahead.
console.log(str.match(/(?<=과).+/g)); // Matches text after '과' using a positive lookbehind.

console.log(str.match(/\\d{3}-\\d{4}-\\d{4}/g)); // Matches phone number patterns.
console.log(str.match(/\\w+@\\w+\\.\\w+/g)); // Matches email address patterns.
// ^ab : matches ab at the beginning of the line
// ab$ : matches ab at the end of the line
// . : matches any one character
// a|b : matches either a or b
// ab? : does not contain b or matches b

const str = `
010-1234-5678
thesecon@gmail.com
Hello world!
<https://www.omdbapi.com/?apikey=7035c60c&s=frozen>
The quick brown fox jumps over the lazy dog.
hello@naver.com
: 1234
동해물과 백두산이 마르고 닳도록
abbcccddddeeeee
`;
console.log(str.match(/^h.../gm)); // [ 'http', 'hell', 'http' ]

console.log(str.match(/\\.com/g)); // [ '.com', '.com', '.com' ]
console.log(str.match(/\\.com$/g)); // null
console.log(str.match(/\\.com$/gm)); // [ '.com', '.com' ]
console.log(str.match(/...\\.com$/gm)); // [ 'ail.com', 'ver.com' ]

console.log(str.match(/fox|dog/g)); // [ 'fox', 'dog' ]
console.log(str.match(/fox|dog|\\.com/g)); // [ '.com', '.com', 'fox', 'dog', '.com' ]

console.log(str.match(/https?/g)); // [ 'https', 'http' ]

// {3} : 3 consecutive matches
// {3, } : 3 or more consecutive matches
// {3, 5} : 3 or more and 5 or less consecutive matches
// + : 1 or more consecutive matches '{1, }'

console.log(str.match(/\\d{3}/g)); // [ '010', '123', '567', '703', '123' ]
console.log(str.match(/d{3}/g)); // [ 'ddd' ]
console.log(str.match(/\\d{3,9}/g)); // [ '010', '1234', '5678', '7035', '1234' ]

// [ab] : a or b
// [a-z] : matches the span of characters from a to z (lowercase English)
// [A-Z] : matches the range of characters from A to Z (English uppercase)
// [0-9] : Matches the range of characters from 0 to 9 (numbers)
// [ga-hyeol]: matches the character range between g and hye (Korean)

console.log(str.match(/[ab]/g));
// [
//     'a', 'b', 'a', 'a',
//     'b', 'a', 'a', 'a',
//     'a', 'b', 'b'
//   ]
console.log(str.match(/[a-z]{1,}/g));
// [
//     'thesecon',        'gmail',
//     'com',             'ello',
//     'world',           'https',
//     'www',             'omdbapi',
//     'com',             'apikey',
//     'c',               'c',
//     's',               'frozen',
//     'he',              'quick',
//     'brown',           'fox',
//     'jumps',           'over',
//     'the',             'lazy',
//     'dog',             'hello',
//     'naver',           'com',
//     'http',            'localhost',
//     'abbcccddddeeeee'
//   ]
console.log(str.match(/[a-zA-Z]+/g));
// [
//     'thesecon',        'gmail',
//     'com',             'Hello',
//     'world',           'https',
//     'www',             'omdbapi',
//     'com',             'apikey',
//     'c',               'c',
//     's',               'frozen',
//     'The',             'quick',
//     'brown',           'fox',
//     'jumps',           'over',
//     'the',             'lazy',
//     'dog',             'hello',
//     'naver',           'com',
//     'http',            'localhost',
//     'abbcccddddeeeee'
//   ]

// \\w : matches 63 characters (word, 52 uppercase and lowercase letters, 10 numbers + _)
// \\b : character boundaries that do not match 63 characters
// \\d : matches any number
// \\s : matches whitespace

console.log(str.match(/\\b[0-9]+\\b/g)); // [ '010', '1234', '5678', '1234' ]
console.log(str.match(/\\s/g));
// [
//     '\\n', '\\n', '\\n', ' ', '\\n',
//     '\\n', ' ',  ' ',  ' ', ' ',
//     ' ',  ' ',  ' ',  ' ', '\\n',
//     '\\n', ' ',  '\\n', ' ', ' ',
//     ' ',  '\\n', '\\n'
//   ]

// (?:): group designation
// (?=) : match forward
// (?<=) : match back

console.log(str.match(/https?:\\/\\/\\w+\\.?/g)); // [ '.', '' ]
console.log(str.match(/https?:\\/\\/(?:\\w+\\.?)+\\/?/g)); // [ '<https://www.omdbapi.com/>', '' ]

console.log(str.match(/.+(?=과)/g)); // [ '동해물' ]
console.log(str.match(/(?<=과).+/g)); // [ ' 백두산이 마르고 닳도록' ]

//
console.log(str.match(/\\d{3}-\\d{4}-\\d{4}/g)); // [ '010-1234-5678' ]
console.log(str.match(/\\w+@\\w+\\.\\w+/g)); // [ 'thesecon@gmail.com', 'hello@naver.com' ]
728x90
Comments