πŸ† Javascript(3) μ •κ·œν‘œν˜„μ‹(0) κΈ°λ³Έ μ‚¬μš©λ²•

Lee JooamΒ·2022λ…„ 4μ›” 29일
0

μ •κ·œν‘œν˜„μ‹μ€ λ¬Έμžμ—΄μ—μ„œ νŠΉμ • 쑰합을 μΆ”μΆœν•˜κΈ° μœ„ν•œ νŒ¨ν„΄μ΄λ‹€.

μ •κ·œν‘œν˜„μ‹λ„ 객체둜 μ·¨κΈ‰λ˜λ©°, 주둜 객체 λ‚΄μž₯ λ©”μ†Œλ“œ λ˜λŠ” λ¬Έμžμ—΄ λ©”μ†Œλ“œμ™€ ν•¨κ»˜ 쓰인닀.

μ •κ·œν‘œν˜„μ‹ 생성

const myReg = new RegExp("pattern", "flags");
const myLitReg = /pattern/flags;

λ¦¬ν„°λŸ΄ ν‘œν˜„μ‹μ€ νŒ¨ν„΄μ΄ λ¬Έμžμ—΄ ν˜•μ‹μœΌλ‘œ 듀어가지 μ•ŠλŠ”λ‹€. κ·Έλ ‡κΈ° λ•Œλ¬Έμ— ν…œν”Œλ¦Ώ λ¦¬ν„°λŸ΄μ„ μ΄μš©ν•œ 동적인 생성이 λΆˆκ°€λŠ₯ν•˜λ‹€.

νŒ¨ν„΄μ΄ 확정적일 λ•Œ λ¦¬ν„°λŸ΄ ν‘œν˜„μ‹μ„ μ‚¬μš©ν•˜λ©΄ μ’‹λ‹€.

문자 검색, 문자 μΆ”μΆœ, 문자 λŒ€μ²΄ λ“± λ‹€μ–‘ν•˜κ²Œ 쓰일 수 있기 λ•Œλ¬Έμ— μ •κ·œμ‹μ— μ΅μˆ™ν•΄μ§€λŠ” 게 μ’‹λ‹€.

πŸ“… νŒ¨ν„΄

const str = `Hi there, Nice to meet you. And Hello there and hi.
I love grey(gray) color not a gry, graay and graaay.
Ya ya YaYaYa Ya
`;

const regExp = /Hi/;

console.log(str.match(regExp));
[
  'Hi',
  index: 0,
  input: 'Hi there, Nice to meet you. And Hello there and hi.\n' +
    'I love grey(gray) color not a gry, graay and graaay.\n' +
    'Ya ya YaYaYa Ya\n',
  groups: undefined
]

νŒ¨ν„΄μ€ 말 κ·ΈλŒ€λ‘œ μ°ΎμœΌλ €λŠ” 문자 νŒ¨ν„΄μ΄λ‹€. λ¬Έμžκ°€ κ·ΈλŒ€λ‘œ 와도 되고 ν›„μˆ ν•  문자 ν”Œλž˜κ·Έμ˜ ν˜•νƒœλ‘œ 올 μˆ˜λ„ μžˆλ‹€.

πŸ“… ν”Œλž˜κ·Έ

const str = `Hi there, Nice to meet you. And Hello there and hi.
I love grey(gray) color not a gry, graay and graaay.
Ya ya YaYaYa Ya
`;

const regExp = /Hi/;
const newRegExp = /Hi/gi;

console.log(str.match(regExp));
console.log(str.match(newRegExp));
[
  'Hi',
  index: 0,
  input: 'Hi there, Nice to meet you. And Hello there and hi.\n' +
    'I love grey(gray) color not a gry, graay and graaay.\n' +
    'Ya ya YaYaYa Ya\n',
  groups: undefined
]
[ 'Hi', 'hi' ]

ν”Œλž˜κ·ΈλŠ” μœ„ μ˜ˆμ‹œμ˜ newRegExp처럼 λ¦¬ν„°λŸ΄μΌ 경우 μ •κ·œμ‹μ˜ 뒀에 뢙인닀. 주둜 μ‚¬μš©λ˜λŠ” ν”Œλž˜κ·ΈλŠ” λ‹€μŒκ³Ό κ°™λ‹€.

  • g, λ§€μΉ­λ˜λŠ” λͺ¨λ“  λ¬Έμžμ—΄ μ°ΎκΈ°, μ—†μœΌλ©΄ 첫 번째 결과만 λ°˜ν™˜
  • m, 멀티라인, λ¬Έμž₯의 μ‹œμž‘μ΄λ‚˜ 끝을 μ°Έμ‘°ν•˜λŠ” νŒ¨ν„΄μΌ μ‹œ 쀄 λ³„λ‘œ 찾음
  • i, 문자 λŒ€μ†Œλ¬Έμž ꡬ뢄 μ•ˆν•¨

πŸ“… 액컀

const str = `Hello there, Nice to meet you. And Hello there and hi. Bye~
Hello I love grey(gray) color not a gry Bye~, Hello graay and graaay. Bye~
Hello Ya ya Hello YaYaYa Bye~ Ya Bye~
`;

const shortStr0 = `Hello my name is Lee.`;
const shortStr1 = `Hello my name is Lee. See u later`;

const regExp0 = /^Hello/g;
const regExp1 = /^Hello/gm;
const regExp2 = /Bye~$/gm;
const regExp3 = /^Hello my name is Lee.$/;

console.log(str.match(regExp0));
console.log(str.match(regExp1));
console.log(str.match(regExp2));
console.log(shortStr0.match(regExp3));
console.log(shortStr1.match(regExp3));
[ 'Hello' ]
[ 'Hello', 'Hello', 'Hello' ]
[ 'Bye~', 'Bye~', 'Bye~' ]
[
  'Hello my name is Lee.',
  index: 0,
  input: 'Hello my name is Lee.',
  groups: undefined
]
null

주어진 μ •κ·œμ‹μ—μ„œ ^ λ¬ΈμžλŠ” ν…μŠ€νŠΈμ˜ μ‹œμž‘ μœ„μΉ˜μ— λŒ€μ‘ν•œλ‹€. κ·Έλ ‡κΈ° λ•Œλ¬Έμ— str의 Hello μ€‘μ—μ„œλ„ 문자의 μ‹œμž‘ λΆ€λΆ„μ—λ§Œ λ§€μΉ­λœλ‹€.

μ΄λ•Œ regExp1의 경우 m μ˜΅μ…˜μ„ μ‚¬μš©ν–ˆλŠ”λ°, 쀄 λ‹¨μœ„λ‘œ νƒμƒ‰ν•˜κΈ° λ•Œλ¬Έμ— Helloκ°€ 3개 맀칭됐닀.

$ λ¬ΈμžλŠ” λ¬Έμž₯의 끝과 λŒ€μ‘λœλ‹€. regExp2의 μ‹€ν–‰ κ²°κ³Όλ₯Ό 보면 m μ˜΅μ…˜μ„ 따라 쀄 λ‹¨μœ„λ‘œ 끝 λΆ€λΆ„μ˜ Bye~만 λ°˜ν™˜ν•˜λŠ” 것을 λ³Ό 수 μžˆλ‹€.

^와 $을 ν•¨κ»˜ μ‚¬μš©ν•˜λ©΄ λ¬Έμž₯이 μ •κ·œμ‹κ³Ό μ™„μ „νžˆ μΌμΉ˜ν•  경우만 맀칭된 κ²ƒμœΌλ‘œ κ°„μ£Όν•œλ‹€.

shortStr0κ³Ό shortStr1μ—μ„œ regExp3의 λ°˜ν™˜ κ²°κ³Όλ₯Ό 보면 그것을 확인할 수 μžˆλ‹€.

πŸ“… 문자 클래슀

μ •κ·œμ‹μ—μ„œλŠ” 문자 클래슀 κΈ°λŠ₯을 μ§€μ›ν•œλ‹€.

const str = `
This_is my example string!!! Put Your Hands Up.
[1, 2, 3, 4, 5] is Array and Array is      one      of { Object }.
ν•œκΈ€λ„ λ˜λ‚˜μš”...?
`;

const wordRegExp = /\w+/g;
const numberRegExp = /\d+/g;
const spaceRegExp = /\s+/g;

console.log(str.match(wordRegExp));
console.log(str.match(numberRegExp));
console.log(str.match(spaceRegExp));
[
  'This_is', 'my',    'example',
  'string',  'Put',   'Your',
  'Hands',   'Up',    '1',
  '2',       '3',     '4',
  '5',       'is',    'Array',
  'and',     'Array', 'is',
  'one',     'of',    'Object'
]
[ '1', '2', '3', '4', '5' ]
[
  '\n', ' ',  ' ',      ' ',
  ' ',  ' ',  ' ',      ' ',
  '\n', ' ',  ' ',      ' ',
  ' ',  ' ',  ' ',      ' ',
  ' ',  ' ',  '      ', '      ',
  ' ',  ' ',  ' ',      '\n',
  ' ',  '\n'
]

\wλŠ” 언더바(_)λ₯Ό ν¬ν•¨ν•œλ¬Έμž(0~9, a~z, A~Z)와 λ§€μΉ­λ˜λŠ” 문자 ν΄λž˜μŠ€μ΄λ‹€. μˆ«μžμ™€ 언더바와 λ§€μΉ­λœλ‹€λŠ” 것에 μ£Όμ˜ν•΄μ•Όν•œλ‹€.

\d와 \sλŠ” 결과둜 λ³Ό 수 μžˆλ“―μ΄ 각각 μˆ«μžμ™€ 곡백(μ€„λ°”κΏˆ 포함)κ³Ό λ§€μΉ­λœλ‹€.

문자 ν΄λž˜μŠ€λŠ” λ°˜λŒ€ ν΄λž˜μŠ€λ‘œλ„ μ‚¬μš© κ°€λŠ₯ν•œλ° μ‚¬μš©λ²•μ€ λ‹€μŒκ³Ό κ°™λ‹€.

const str = `
This_is my example string!!! Put Your Hands Up.
[1, 2, 3, 4, 5] is Array and Array is      one      of { Object }.
ν•œκΈ€λ„ λ˜λ‚˜μš”...?
`;

const notWordRegExp = /\W+/g;
const notNumberRegExp = /\D+/g;
const notSpaceRegExp = /\S+/g;

console.log(str.match(notWordRegExp));
console.log(str.match(notNumberRegExp));
console.log(str.match(notSpaceRegExp));
[
  '\n',
  ' ',
  ' ',
  ' ',
  '!!! ',
  ' ',
  ' ',
  ' ',
  '.\n[',
  ', ',
  ', ',
  ', ',
  ', ',
  '] ',
  ' ',
  ' ',
  ' ',
  ' ',
  '      ',
  '      ',
  ' { ',
  ' }.\nν•œκΈ€λ„ λ˜λ‚˜μš”...?\n'
]
[
  '\nThis_is my example string!!! Put Your Hands Up.\n[',
  ', ',
  ', ',
  ', ',
  ', ',
  '] is Array and Array is      one      of { Object }.\nν•œκΈ€λ„ λ˜λ‚˜μš”...?\n'
]
[
  'This_is',    'my',    'example',
  'string!!!',  'Put',   'Your',
  'Hands',      'Up.',   '[1,',
  '2,',         '3,',    '4,',
  '5]',         'is',    'Array',
  'and',        'Array', 'is',
  'one',        'of',    '{',
  'Object',     '}.',    'ν•œκΈ€λ„',
  'λ˜λ‚˜μš”...?'
]

κ²°κ³Όμ—μ„œ λ³Ό 수 μžˆλ“―μ΄ 각각을 λŒ€λ¬Έμžλ‘œ μ μš©ν•˜λ©΄ λ°˜λŒ€ 값이 λ§€μΉ­λœλ‹€.

문자 ν΄λž˜μŠ€μ—λŠ” .도 μžˆλŠ”λ°, .은 μ€„λ°”κΏˆμ„ μ œμ™Έν•œ λͺ¨λ“  κ°’λ“€κ³Ό λ§€μΉ­λœλ‹€.

πŸ“… \b, Word Boundary

const str = `
This_is my example string!!!
byebyebye
`;

const regExp0 = new RegExp('bye', 'g');
const regExp1 = /\bbye/g;
const regExp2 = /bye\b/g;
const regExp3 = /bye\B/g;

console.log(str.match(regExp0));
console.log(str.match(regExp1));
console.log(str.match(regExp2));
console.log(str.match(regExp3));
[ 'bye', 'bye', 'bye' ]
[ 'bye' ]
[ 'bye' ]
[ 'bye', 'bye' ]

word boundaryλŠ” 문자의 경계와 λ§€μΉ­λœλ‹€. μ‰½κ²Œ μƒκ°ν•˜λ©΄ \wκ°€ μ•„λ‹Œ 녀석이라고 μƒκ°ν•˜λ©΄ λœλ‹€.

좜처: https://ko.javascript.info/regexp-boundary

\b도 λ°˜λŒ€ 클래슀처럼 λŒ€λ¬Έμžλ₯Ό μ΄μš©ν•˜λ©΄ word boundaryκ°€ μ•„λ‹Œ 것과 λ§€μΉ­λ˜λŠ”λ° regExp3의 맀칭 κ²°κ³Όλ₯Ό 톡해 확인할 수 μžˆλ‹€.

끝이 word boundaryκ°€ μ•„λ‹Œ 첫 bye와 두 번째 byeκ°€ λ§€μΉ­λœλ‹€.

그런데 μ™œ \bλ₯Ό μ¨μ•Όν• κΉŒ? \Wλ₯Ό 써도 될텐데 말이닀.

const str = `
This_is my example string!!!
byebyebye
`;

const regExp0 = new RegExp('bye', 'g');
const regExp1 = /\Wbye/g;
const regExp2 = /bye\W/g;
const regExp3 = /bye\W/g;

console.log(str.match(regExp0));
console.log(str.match(regExp1));
console.log(str.match(regExp2));
console.log(str.match(regExp3));
[ 'bye', 'bye', 'bye' ]
[ '\nbye' ]
[ 'bye\n' ]
[ 'bye\n' ]

κ·Έ μ΄μœ λŠ” λ‹€μŒμ˜ κ²°κ³Όμ—μ„œ 직접 확인할 수 μžˆλ‹€. 😐

πŸ“… Set, range

const str = `
12345678
This_is my_example_
`;

const regExp0 = /[0-9]+/g;
const regExp1 = /[a-zA-Z]+/g;
const regExp2 = /[Tiem]+/g;

console.log(str.match(regExp0));
console.log(str.match(regExp1));
console.log(str.match(regExp2));
[ '12345678' ]
[ 'This', 'is', 'my', 'example' ]
[
  'T', 'i', 'i',
  'm', 'e', 'm',
  'e'
]

λŒ€κ΄„ν˜Έλ₯Ό 톡해 Setκ³Ό range κΈ°λŠ₯을 ν™œμš©ν•  수 μžˆλ‹€. 0-9λŠ” 숫자 0 λΆ€ν„° 9, a-zλŠ” a λΆ€ν„° z λ“± λ‹€μ–‘ν•˜κ²Œ ν™œμš©ν•  수 μžˆλ‹€.

λ‹€λ§Œ μ•„μŠ€ν‚€ μ½”λ“œ 값을 μ°Έμ‘°ν•˜λ‹ˆ μ‚¬μš©μ— μœ μ˜ν•΄μ•Όν•œλ‹€.

regExp2 같은 κ²½μš°λŠ” μ•ŒνŒŒλ²³ T, i, e, m 쀑 ν•˜λ‚˜μ— λ§€μΉ­λœλ‹€λŠ” λœ»μ΄λ‹€.

πŸ“… μˆ˜λŸ‰μž(Quentifiers)

const str = `
010-898-0893
02-405-3412
010 405 3412
`;

const regExp = /\d{2,3}[- ]?\d{3}[- ]?\d{4}/g;

console.log(str.match(regExp));
[ '010-898-0893', '02-405-3412', '010 405 3412' ]

μˆ˜λŸ‰μžλŠ” λ§€μΉ­λ˜λŠ” νŒ¨ν„΄μ˜ 일정 개수λ₯Ό ν‘œν˜„ν•  λ•Œ μ‚¬μš©ν•œλ‹€. μ€‘κ΄„ν˜Έ μ•ˆμ— 숫자λ₯Ό λ„£μ–΄μ„œ μ‚¬μš©ν•˜λŠ”λ° μ˜λ―ΈλŠ” λ‹€μŒκ³Ό κ°™λ‹€.

{0, } 0 이상
{1, 3} 1 이상 3 μ΄ν•˜

μˆ˜λŸ‰μžλŠ” κ°„νŽΈν•˜κ²Œ ν‘œν˜„ν•˜λŠ” shorthand λ˜ν•œ μ œκ³΅ν•œλ‹€.

μˆ˜λŸ‰μžμ˜λ―Έν‘œν˜„
*0개 이상{0,}
?0 λ˜λŠ” 1{0,1}
+1개 이상{1,}
profile
ν”„λ‘ νŠΈμ—”λ“œ 개발자둜 κ±Έμ–΄κ°€λŠ” μ€‘μž…λ‹ˆλ‹€.

0개의 λŒ“κΈ€