๐Ÿง  codingtest/์ •๊ทœํ‘œํ˜„์‹(regexp)

02. ๋ฌธ์ž ํ•˜๋‚˜ ์ฐพ๊ธฐ!

awesomeyelim 2022. 8. 27. 18:56
728x90

๋ชจ๋“  ๋ฌธ์ž ์ฐพ๊ธฐ

  1. ๋งˆ์นจํ‘œ(.) : ์•„๋ฌด ๋ฌธ์ž ํ•˜๋‚˜ ์™€ ์ผ์น˜
const str = "Hello, my name is Yelim. I really hope you guys have good day~!";

function test(e) {
  return console.log(e.match(".lim")[0]);
}

test(str); // elim
  1. ์—ฐ์† ๋งˆ์นจํ‘œ ๋‘๊ฐœ (..) : ๋ถ™์–ด์žˆ๋Š” ๋ฌธ์ž ๋‘๊ฐœ ์™€ ์ผ์น˜
const str = "Hello, my name is Yelim. I really hope you guys have good day~!";

function test(e) {
  return console.log(e.match("..lim")[0]);
}

test(str); // Yelim
  1. ์ด์Šค์ผ€์ดํ”„(\) ์‚ฌ์šฉํ•œ ๋งˆ์นจํ‘œ(.) : ๋งˆ์นจํ‘œ(.) ์ž์ฒด๋ฅผ ์ฐพ์„์ˆ˜ ์žˆ๋‹ค.
const str = "Hello, my name is Yelim. I really hope you guys have good day~!";

function test(e) {
  return console.log(e.match("m\\.")[0]);
} // ์—ญ์Šฌ๋ž˜์‹œ(\) ์ž์ฒด๊ฐ€ ์ด์Šค์ผ€์ดํ”„ ๋˜๋ฏ€๋กœ ์ธ์‹์ด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ฌธ์ž๋ฅผ ์ด์Šค์ผ€์ดํ”„ ํ•ด์ค„๋•Œ๋Š” ์—ญ์Šฌ๋ž˜์‹œ(\) ๋‘๋ฒˆ์‚ฌ์šฉ

test(str); // m.
const arr = [
  "yelim1.jpg",
  "nun2.jpg",
  "dong1.xls",
  "don2.xls",
  "dong3.xls",
  "don4.xls",
  "sal.png",
  "sal2.png",
];

function test(e) {
  for (let i of e) {
    i.match("don..") && console.log(i.match("don..")[0]);
  }
}

test(arr);
// dong1
// don2.
// dong3
// don4.

์—ญ์Šฌ๋ž˜์‹œ(\)

๋ฉ”ํƒ€ ๋ฌธ์ž๋‹ค(๋ฌธ์ž ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ณ  ํŠน๋ณ„ํ•œ ์˜๋ฏธ๋ฅผ ์ง€๋‹ˆ๋Š” ๋ฌธ์ž๋ฅผ ์นญํ•จ)

 

๋ถ€ํ˜ธ ๊ธฐ๋Šฅ
. ์•„๋ฌด ๋ฌธ์ž ํ•˜๋‚˜ ์™€ ์ผ์น˜
.. ๋ถ™์–ด์žˆ๋Š” ๋ฌธ์ž ๋‘๊ฐœ ์™€ ์ผ์น˜
\. ์ž์ฒด๋ฅผ ์ฐพ์„์ˆ˜ ์žˆ๋‹ค.