๐Ÿ“’Javascript RegExp :)

zooyahoยท2022๋…„ 4์›” 8์ผ
0

study with me

๋ชฉ๋ก ๋ณด๊ธฐ
8/19
post-thumbnail

์ •๊ทœํ‘œํ˜„์‹

  • ์ผ์ •ํ•œ ํŒจํ„ด์„ ๊ฐ€์ง„ ๋ฌธ์ž์—ด์˜ ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํ˜•์‹์–ธ์–ด
  • ๋ฌธ์ž์—ด์„ ๋Œ€์ƒ์œผ๋กœ ํŒจํ„ด ๋งค์นญ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•จ.

๐Ÿ”ต ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹์œผ๋กœ ์ƒ์„ฑ

/pattern/[, flags]

let regexp = /the/gi;

๐Ÿ”ต RegExp์ƒ์„ฑ์ž ํ•จ์ˆ˜

new RegExp(pattern,[, flags])

const target = 'Is this all there is';

const regexp = new regExp(/is/i); // ES6
//const regexp = new regExp(/is/,'i');
//const regexp = new regExp('is',i);

regexp.test(target); // true

๐Ÿ”ต RegExp ๋ฉ”์„œ๋“œ

RegExp.prototype.exec()

  • ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด ์ •๊ทœ ํ‘œํ˜„์‹์˜ ํŒจํ„ด์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๋งค์นญ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
  • ๋งค์นญ ๊ฒฐ๊ณผ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ null์„ ๋ฐ˜ํ™˜.

RegExp.prototype.test()

๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜

const target = 'Is this all there is';

regexp = /is/gi;
console.log(regexp.test(target)); // true

String.prototype.match()

  • ๋Œ€์ƒ ๋ฌธ์ž์—ด๊ณผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ์˜ ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
  • exec๋ฉ”์„œ๋“œ๋Š” gํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์ฒซ๋ฒˆ์งธ ๋งค์นญ๊ฒฐ๊ณผ๋งŒ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ match๋ฉ”์„œ๋“œ๋Š” gํ”Œ๋ž˜๊ทธ ์ง€์ • ์‹œ ๋ชจ๋“  ๋งค์นญ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•จ.
const regexp = /is/g;
target.match(regexp); // ["is","is"]

String.prototype.replace()

  • ์ฒซ๋ฒˆ์งธ ์ธ์ž์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž๊ฐ€ ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ ๋ณ€๊ฒฝ๋จ.
  • ์›๋ณธ์ด ์ˆ˜์ •๋˜์ง€ ์•Š์Œ.
const regexp = /is/gi;
console.log("๊ต์ฒด", target.replace(regexp, "Rabbit"));
// ๊ต์ฒด Rabbit thRabbit all there Rabbit
console.log("์›๋ณธ", target);
// ์›๋ณธ Is this all there is

๐Ÿ”ต ํ”Œ๋ž˜๊ทธ

  • ํ•˜๋‚˜ ์ด์ƒ์˜ ํ”Œ๋ž˜๊ทธ๋ฅผ ๋™์‹œ์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ.
  • ์–ด๋– ํ•œ ํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•ด์„œ ํŒจํ„ด์„ ๊ฒ€์ƒ‰ํ•˜๊ณ , ์ฒซ๋ฒˆ์งธ ๋งค์นญํ•œ ๋Œ€์ƒ๋งŒ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์ข…๋ฃŒํ•จ.
  • i(Ignore case): ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜์ง€ ์•Š๊ณ  ํŒจํ„ด์„ ๊ฒ€์ƒ‰.
  • g(Global): ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜๊ณ  ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ์ „์—ญ ๊ฒ€์ƒ‰ํ•จ.
  • m(Multi line): ๋ฌธ์ž์—ด์˜ ํ–‰์ด ๋ฐ”๋€Œ๋”๋ผ๋„ ํŒจํ„ด๊ฒ€์ƒ‰์„ ๊ณ„์†ํ•จ. ๋ฌธ์ž์—ด์—์„œ ์ค„๋ฐ”๊ฟˆ์ด ๋˜์–ด์–ด์ ธ ์žˆ๋Š” ์ค„๋งˆ๋‹ค ์ฒดํฌํ•จ!!

๐Ÿ‘พ#01

const str = `
010-1234-5678
thesecon@gmail.com
https://www.omdbapi.com/?apikey=7035c60c&s=frozen
The quick brown fox jumps over the lazy dog.
abbcccdddd
http://localhost:1234
`;
let regexp = /the/;
let regexpG = /the/g; // ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ๋ฐฐ์—ด๋กœ ์ถœ๋ ฅ
let regexpI = /the/i; // ๋Œ€์†Œ๋ฌธ์ž ๋ฌด์‹œํ•˜๊ณ  ์ฒดํฌํ•จ
let regexpGi = /the/gi;

console.log("match-ํ”Œ๋ž˜๊ทธX", str.match(regexp));
console.log("match-gํ”Œ๋ž˜๊ทธ", str.match(regexpG));
console.log("match-iํ”Œ๋ž˜๊ทธ", str.match(regexpI));
console.log("match-giํ”Œ๋ž˜๊ทธ", str.match(regexpGi));

๐Ÿ‘พ#02 - $ : ๋ฌธ์žฅ์˜ ๋์„ ๋œปํ•จ.

/* ๋ฌธ์žฅ ๋์— .(์˜จ์ )์ด ์žˆ๋Š”์ง€ ์ฒดํฌ */
console.log(str.match(/\.$/gi)); // ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด ๋์— ํ•œ๋ฒˆ๋งŒ ์ฒดํฌํ•จ
console.log(str.match(/\.$/gim)); // ๋ฌธ์ž์—ด์—์„œ ์ค„๋ฐ”๊ฟˆ์ด ๋˜์–ด์–ด์ ธ ์žˆ๋Š” ์ค„๋งˆ๋‹ค ์ฒดํฌํ•จ!!

๐Ÿ”ต ํŒจํ„ด

ํŒจํ„ด์€ /๋กœ ์—ด๊ณ  ๋‹ซ์œผ๋ฉฐ ๋ฌธ์ž์—ด์˜ ๋”ฐ์˜ดํ‘œ๋Š” ์ƒ๋žตํ•จ.

  • ^ : [...]๋‚ด์˜ ^์€ Not์˜ ์˜๋ฏธ, [...] ๋ฐ–์—์„œ์˜ ^๋Š” ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘์„ ์˜๋ฏธ
  • $ : ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰์„ ์˜๋ฏธ
  • .: ์ž„์˜์˜ ๋ฌธ์ž ํ•œ ๊ฐœ๋ฅผ ์˜๋ฏธ(๊ณต๋ฐฑ ํฌํ•จ!)
  • + : ์•ž์„  ํŒจํ„ด์ด ์ตœ์†Œ ํ•œ๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์„ ์˜๋ฏธ, {1,}๊ณผ ๊ฐ™์€ ์˜๋ฏธ
  • ? : ์•ž์„  ํŒจํ„ด์ด ์ตœ๋Œ€ ํ•œ๋ฒˆ(0ํฌํ•จ) ์ด์ƒ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์„ ์˜๋ฏธ. {0,1}๊ณผ ๊ฐ™์Œ, ab? : b๊ฐ€ ์—†๊ฑฐ๋‚˜ b์™€ ์ผ์น˜
  • * : ๋ฐ˜๋ณต ์—ฌ๋ถ€ ํ‘œํ˜„ - (aa)* (aa) ๊ฐ€ 0๊ฐœ ~ infinite๊นŒ์ง€ ๋ชจ๋‘ ๊ฐ€๋Šฅ
  • | : or๊ณผ ๊ฐ™์€ ์˜๋ฏธ, a|b : a ๋˜๋Š” b์™€ ์ผ์น˜
  • [abc] : a๋˜๋Š” b๋˜๋Š” c
  • [a-z] : a๋ถ€ํ„ฐ z์‚ฌ์ด์˜ ๋ฌธ์ž ๊ตฌ๊ฐ„์— ์ผ์น˜(์˜์–ด ์†Œ๋ฌธ์ž)
  • [A-Z] : A๋ถ€ํ„ฐ Z์‚ฌ์ด์˜ ๋ฌธ์ž ๊ตฌ๊ฐ„์— ์ผ์น˜(์˜์–ด ์†Œ๋ฌธ์ž)
  • [0-9] : 0๋ถ€ํ„ฐ 9์‚ฌ์ด์˜ ๋ฌธ์ž ๊ตฌ๊ฐ„์— ์ผ์น˜(์ˆซ์ž)
  • [๊ฐ€-ํžฃ] : ๊ฐ€๋ถ€ํ„ฐ ํžฃ์‚ฌ์ด์˜ ๋ฌธ์ž ๊ตฌ๊ฐ„์— ์ผ์น˜(ํ•œ๊ธ€)
  • \s : ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ณต๋ฐฑ๋ฌธ์ž(์ŠคํŽ˜์ด์Šค, ํƒญ ๋“ฑ)๋ฅผ ์˜๋ฏธํ•จ. [\t\r\n\v\f]์™€ ๊ฐ™์€ ์˜๋ฏธ.
  • \b : 63๊ฐœ ๋ฌธ์ž์— ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ž ๊ฒฝ๊ณ„(Boundary). ex) \b\w{2,3}\b -> 010-123-1234์—์„œ ['010', '123']์„ ๋ฐ˜ํ™˜ํ•จ.
  • \d : ์ˆซ์ž(Disit)์— ์ผ์น˜, [0-9]์™€ ๊ฐ™์€ ์˜๋ฏธ
  • \D : ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋ฅผ ์˜๋ฏธ, [^0-9]
  • \w : 63๊ฐœ ๋ฌธ์ž(Wold, ๋Œ€์†Œ์˜๋ฌธ52๊ฐœ + ์ˆซ์ž10๊ฐœ + _)์— ์ผ์น˜. ์ฆ‰ [A-Za-z0-9_]์™€ ๊ฐ™์€ ์˜๋ฏธ
  • \W : ์•ŒํŒŒ๋ฒณ, ์ˆซ์ž, ์–ธ๋”์Šค์ฝ”์–ด๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋ฅผ ์˜๋ฏธ, [^A-Za-z0-9_]
  • {m,n} : ๋ฐ˜๋ณต๊ฒ€์ƒ‰, ์ตœ์†Œ m๋ฒˆ, ์ตœ๋Œ€ n๋ฒˆ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์„ ์˜๋ฏธ. ๋’ค์— ๊ณต๋ฐฑ์ด ์žˆ์œผ๋ฉด ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์Œ.
  • {n} : n๋ฒˆ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์„ ์˜๋ฏธ, {n,n}๊ณผ ๊ฐ™์€ ์˜๋ฏธ.
  • {n,} : ์ตœ์†Œ n๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์„ ์˜๋ฏธ.
  • (?=) : ์•ž์ชฝ ์ผ์น˜(Lookahead)
  • (?<=) : ๋’ค์ชฝ ์ผ์น˜(Lookabehind)

๐Ÿ‘พ #01 @๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์•ž, ๋’ค ๋ฌธ์ž ๋ฐ˜ํ™˜ํ•˜๊ธฐ

const str = 'thesecon@gmail.com';
console.log(str.match(/.{1,}(?=@)/g)); // ['thesecon']
console.log(str.match(/(?<=@).{1,}/g)); // ['gmail.com']

๐Ÿ‘พ #02 ๋ถ„ํ•ด๋˜์ง€ ์•Š์€ ๋‹จ์–ด ๋ ˆ๋ฒจ๋กœ ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” +๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉ

const target = 'A AA B BB Aa Bb';
const regexp = /A+|B+/g;

target.match(regexp); // ["A", "AA", "B", "BB", "Aa", "Bb"]

๐Ÿ‘พ #03 []๋‚ด์˜ ๋ฌธ์ž๋Š” or๋กœ ๋™์ž‘ํ•œ๋‹ค. ๋’ค์— +๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ž์„  ํŒจํ„ด์„ ํ•œ๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณตํ•œ๋‹ค.

const regexp = /[AB]+/g;

target.match(regexp); // ["A", "AA", "B", "BB", "Aa", "Bb"]

๐Ÿ‘พ #04 ๋ฒ”์œ„ ์ง€์ •ํ•˜๋ ค๋ฉด []๋‚ด์— -์„ ์‚ฌ์šฉ.

// ๋Œ€๋ฌธ์ž๋งŒ
let regexp = /[A-Z]/g;
target.match(regexp); // ["A", "AA", "B", "BB", "A", "B"]

//๋Œ€์†Œ๋ฌธ์ž
regexp = /[A-Za-z]/g;
target.match(regexp); // ["A", "AA", "B", "BB", "Aa", "Bb"]

๐Ÿ”ต ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ •๊ทœํ‘œํ˜„์‹

๐Ÿ“Ž ํŠน์ • ๋‹จ์–ด๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ

const url = 'https://example.com';
/^https?:\/\//.test(url); // true
/^(http|https):\/\//.test(url); // true

๐Ÿ“Ž ํŠน์ • ๋‹จ์–ด๋กœ ๋๋‚˜๋Š”์ง€ ๊ฒ€์‚ฌ

const fileName = 'index.html';
/html$/.test(fileName); // true

๐Ÿ“Ž ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ธ์ง€ ๊ฒ€์‚ฌ

const target = '12345'
/^\d+$/.test(target); // true

๐Ÿ“Ž ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ณต๋ฐฑ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ

const target = ' hi'
console.log(/^[\s]+/.test(target)); // true
console.log(/^\s+/.test(target)); // true

๐Ÿ“Ž ์•„์ด๋””๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ์ง€ ๊ฒ€์‚ฌ

// ์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž๋กœ ์‹œ์ž‘ํ•˜๊ณ  ๋๋‚˜๋ฉฐ 4~10์ž๋ฆฌ์ธ์ง€ ๊ฒ€์‚ฌ
const id = 'abc123'
console.log(/^[A-Za-z0-9]{4,10}$/.test(id)); // true

๐Ÿ“Ž ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ํ˜•์‹์— ๋งž๋Š”์ง€ ๊ฒ€์‚ฌ

const cellphone = '010-1234-5678';
/^\d{3}-\d{4}-\d{4}$/.target(cellphone); // true

๐Ÿ“Ž ํŠน์ˆ˜ ๋ฌธ์ž ํฌํ•จ ์—ฌ๋ถ€ ๊ฒ€์‚ฌ

const target = 'abc#123';
(/[^A-Za-z0-9]/gi).test(target); // true

// ํŠน์ˆ˜ ๋ฌธ์ž ์ œ๊ฑฐ
target.replace(/[^A-Za-z0-9]/gi,''); // 'abc123'
profile
์ฆ๊ฒ๊ฒŒ ๊ฐœ๋ฐœํ•˜์ž ์ฅฌ์•ผํ˜ธ๐Ÿ‘ป

0๊ฐœ์˜ ๋Œ“๊ธ€