GiantStepDEV
article thumbnail

์ •๊ทœ ํ‘œํ˜„์‹(regExp)

  • ๋ฌธ์ž์—ด์— ํฌํ•จ๋œ ํŠน์ • ๋ฌธ์ž ์กฐํ•ฉ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํŒจํ„ด ์ž…๋‹ˆ๋‹ค.

๊ธฐ๋ณธ ํ˜•ํƒœ

const regexp = /World/;
const regexp = new RegExp("World");

์˜ˆ์ œ

let str = "Hello World! World!";
const re = /World/;
console.log(re.test(str)); // ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ํฌํ•จ ์—ฌ๋ถ€ ํ™•์ธ
console.log(str.search(re)); // ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ์œ„์น˜ ๋ฐ˜ํ™˜

exec() : ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜

  • ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋œ ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ํŒจํ„ด์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ, ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
  • ๋งŒ์•ฝ, ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ์—†์œผ๋ฉด null ๋ฐ˜ํ™˜
let targetStr =
  "๊ฐ€์žฅ ํฐ ์‹ค์ˆ˜๋Š” ํฌ๊ธฐ, ๊ฐ€์žฅ ์–ด๋ฆฌ์„์€ ์ผ์€ ๋‚จ์˜ ๊ฒฐ์ • ์ฐพ๊ธฐ, ๊ฐ€์žฅ ์ข‹์€ ์„ ๋ฌผ์€ ์šฉ์„œ";
let firstRst = /๊ฐ€์žฅ+/.exec(targetStr); // ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ
let secondRst = /๊ฐ€์žฅํฐ+/.exec(targetStr);
console.log(firstRst);
console.log(`exec() : ${secondRst}`);

test() : ๋ฌธ์ž์—ด ์žˆ์œผ๋ฉด true, ์—†์œผ๋ฉด false

  • ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋œ ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
  • ๋ฌธ์ž์—ด์ด ์žˆ์œผ๋ฉด true, ์—†์œผ๋ฉด false ๋ฐ˜ํ™˜
let targetStr2 =
  "๊ฐ€์žฅ ํฐ ์‹ค์ˆ˜๋Š” ํฌ๊ธฐ, ๊ฐ€์žฅ ์–ด๋ฆฌ์„์€ ์ผ์€ ๋‚จ์˜ ๊ฒฐ์ • ์ฐพ๊ธฐ, ๊ฐ€์žฅ ์ข‹์€ ์„ ๋ฌผ์€ ์šฉ์„œ";
let firstRst2 = /๊ฐ€์žฅ+/.test(targetStr2); // ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ
let secondRst2 = /๊ฐ€์žฅํฐ+/.test(targetStr2);
console.log(`test() : ${firstRst2}`);
console.log(`test() : ${secondRst2}`);

์ „ํ™”๋ฒˆํ˜ธ ๊ฒ€์‚ฌํ•˜๊ธฐ

  • \d : ์ˆซ์ž ์˜๋ฏธ
  • {} : {} ์•ˆ์˜ ์ˆซ์ž๋Š” ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธ
let inputNum = "010-1234-5678";
const regex = /\d{3}-\d{4}-\d{4}$/;
let rst = regex.test(inputNum);
if (rst) {
  console.log("๋กœ๊ทธ์ธ ์„ฑ๊ณต!");
} else console.log("๋กœ๊ทธ์ธ ์‹คํŒจ! ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ์— ํ•˜์ดํ”ˆ(-)์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");

match() : ์ „ํ™”๋ฒˆํ˜ธ ์ถ”์ถœ

  • ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ํŒจํ„ด ๋งค์นญ์„ ์ˆ˜ํ–‰
const inputText = "์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ๋ฒˆํ˜ธ๋Š” 010-9090-8080 ์ž…๋‹ˆ๋‹ค. ์—ฐ๋ฝ ์ฃผ์„ธ์š”.";
const regex2 = /\d{3}-\d{4}-\d{4}/;
let regNumber = inputText.match(regex2);
console.log(regNumber);
console.log(regNumber[0]);

g : ์ „ํ™”๋ฒˆํ˜ธ ์—ฌ๋Ÿฌ๊ฐœ ์ถ”์ถœ

  • \d{2, 3} : ์ˆซ์ž 2~3๊ฐœ๋กœ ์‹œ์ž‘
  • g : ๋งค์นญ๋˜๋Š” ๋ชจ๋“  ํ•ญ๋ชฉ ์ฐพ๊ธฐ
const inputText2 = "์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ๋ฒˆํ˜ธ๋Š” 010-3212-2323, 032-872-2142 ์ž…๋‹ˆ๋‹ค.";
const regex3 = /\d{2,3}-\d{3,4}-\d{4}/g;
let regNumber2 = inputText2.match(regex3);
// console.log(regNumber2);
// for(let i of regNumber2) console.log(i);
regNumber2.forEach((i) => console.log(i));

์ •๊ทœ์‹ ์ฃผ์š” ํŒจํ„ด ์‚ฌ์šฉ ์˜ˆ์ œ

const regex4 = /์˜ค๋Š˜์˜/; // ๋‹จ์ˆœ ๊ธ€์ž ์ฐพ๊ธฐ
const regex5 = /010/g; // 010์œผ๋กœ ์‹œ์ž‘๋˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด ์ฐพ๊ธฐ
const regex6 = /[a-zA-Z]/g; // ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž ์ฐพ๊ธฐ
const regex7 = /[^0-9]/g; // ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž ๋ชจ๋‘ ์ฐพ๊ธฐ
const regex8 = /[ใ„ฑ-ใ…Ž๊ฐ€-ํžฃ]/g; // ๋ชจ๋“  ํ•œ๊ธ€ ์ฐพ๊ธฐ
const inputText3 = "Hello. ์ œ ๋ฒˆํ˜ธ๋Š” 010-3212-2323, 032-872-2142 ์ž…๋‹ˆ๋‹ค.";
let rst2 = inputText3.match(regex8);
console.log(rst2);

์›น์‚ฌ์ดํŠธ ์ฃผ์†Œ ์ •๊ทœ์‹ ๊ฒ€์‚ฌ

  • ์›น์‚ฌ์ดํŠธ ์ฃผ์†Œ๋Š” http:// ํ˜น์€ https://๋กœ ์‹œ์ž‘ํ•˜๊ณ , ์•ŒํŒŒ๋ฒณ _ - . ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Œ
  • /https?:\/\/[\w\-\.]+/g
const text = "๊ตฌ์ž… ๋ฌธ์˜๋Š” http://gongumarket.com https://google.com 010-2323-2312 ์œผ๋กœ ์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.";
const regex9 = /https?:\/\/[\w\-\.]+/g;
let rst3 = text.match(regex9);
console.log(rst3);

์‹ค์Šต์˜ˆ์ œ - ๋กœ๊ทธ์ธ ์ฐฝ ๋งŒ๋“ค๊ธฐ

JavaScript

const register = document.getElementById("register");
let conFirmCnt = 0;

// ์•„์ด๋”” ํ™•์ธ
let idInput = document.querySelector("#idInput");
const idBtn = document.querySelector("#idBtn");
idBtn.addEventListener("click", function() {
  const errorMsg = document.querySelector("#errorMsg1");
  const regExp = /^[a-z]+[a-z0-9]{8,20}$/g;
  let rst = regExp.test(idInput.value);
  if(rst) {
    errorMsg.style.display = 'none';
    conFirmCnt++;
    alert("ํ™•์ธ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
    document.getElementById("pwdInput").focus();
  } else {
    errorMsg.style.display = 'block';
  }
});

// ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ
let pwdInput = document.querySelector("#pwdInput");
const pwdBtn = document.querySelector("#pwdBtn");
pwdBtn.addEventListener("click", function() {
  const errorMsg = document.querySelector("#errorMsg2");
  const regExp = /^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[$`~!@$!%*#^?&//(\\)\-_=+]).{8,20}$/;
  let rst = regExp.test(pwdInput.value);
  if(rst) {
    errorMsg.style.display = 'none';
    conFirmCnt++;
    alert("ํ™•์ธ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
    document.getElementById("mailInput").focus();
  } else {
    errorMsg.style.display = 'block';
  }
});

// ์ด๋ฉ”์ผ ํ™•์ธ
let mailInput = document.querySelector("#mailInput");
const mailBtn = document.querySelector("#mailBtn");
mailBtn.addEventListener("click", function() {
  const errorMsg = document.querySelector("#errorMsg3");
  const regExp = /^[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,3}$/i;
  let rst = regExp.test(mailInput.value);
  if(rst) {
    errorMsg.style.display = 'none';
    conFirmCnt++;
    alert("ํ™•์ธ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
    document.getElementById("phoneInput").focus();
  } else {
    errorMsg.style.display = 'block';
  }
});

// ์ „ํ™”๋ฒˆํ˜ธ ํ™•์ธ
let phoneInput = document.querySelector("#phoneInput");
const phoneBtn = document.querySelector("#phoneBtn");
phoneBtn.addEventListener("click", function() {
  const errorMsg = document.querySelector("#errorMsg4");
  const regExp = /^01(?:0|1|[6-9])-(?:\d{3}|\d{4})-\d{4}$/;
  let rst = regExp.test(phoneInput.value);
  if(rst) {
    errorMsg.style.display = 'none';
    conFirmCnt++;
    alert("ํ™•์ธ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
    if(conFirmCnt == 4) {
      register.disabled = false;
    }
  } else {
    errorMsg.style.display = 'block';
  }
});

function popUp() {
  window.open('230404_3_๊ฐ€์ž…์™„๋ฃŒ.html', "ํŒ์—…", 'width=500, height=700');
}

const register2 = document.querySelector("#register");
register2.addEventListener("click", popUp);

HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="./230404_3_๋กœ๊ทธ์ธ.css">
  <title>๋กœ๊ทธ์ธ ์˜ˆ์ œ</title>
</head>
<body>
  <h1>ํšŒ์›๊ฐ€์ž…</h1>
  <form>
    <div class="container">
      <div class="item">
        <input type="text" id="idInput" placeholder="์•„์ด๋”” ์ž…๋ ฅ">
        <button id="idBtn">ํ™•์ธ</button>
        <p id="errorMsg1">์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.</p>
      </div>
      <div class="item">
        <input type="password" id="pwdInput" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ">
        <button id="pwdBtn">ํ™•์ธ</button>
        <p id="errorMsg2">์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.</p>
      </div>
      <div class="item">
        <input type="text" id="mailInput" placeholder="์ด๋ฉ”์ผ ์ž…๋ ฅ">
        <button id="mailBtn">ํ™•์ธ</button>
        <p id="errorMsg3">์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.</p>
      </div>
      <div class="item">
        <input type="text" id="phoneInput" placeholder="์ „ํ™”๋ฒˆํ˜ธ ์ž…๋ ฅ">
        <button id="phoneBtn">ํ™•์ธ</button>
        <p id="errorMsg4">์ž˜๋ชป ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.</p>
      </div>
      <button type="submit" id="register" disabled>๊ฐ€์ž… ์š”์ฒญ</button>
    </div>
  </form>
  <script src="./230404_3_๋กœ๊ทธ์ธ.js"></script>
</body>
</html>

CSS

* {
  box-sizing: border-box;
  margin: 0;
}
h1 {
  text-align: center;
  margin: 10px 0;
}
body {
  width: 100%;
  margin: 0 auto;
}
.container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 10px;
}
.item > p {
  text-align: right;
  font-size: 0.8em;
  color: #ff0000;
  display: none;
}
.item > input {
  border-radius: 10px;
  border: 1px solid #696969;
  width: 200px;
  height: 40px;
}
.item > button {
  border-radius: 10px;
  border: 1px solid #696969;
  width: 60px;
  height: 40px;
  background-color: #E5F0FF
}
#register {
  border-radius: 10px;
  border: 1px solid #696969;
  width: 260px;
  height: 40px;
}

์ž˜๋˜๋˜ ์ฝ”๋“œ๊ฐ€ ๊ฐ‘์ž๊ธฐ ์™œ ์•ˆ๋˜๋‚˜ ํ–ˆ๋”๋‹ˆ formํƒœ๊ทธ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ submit ์†์„ฑ์ด ์žˆ์–ด์„œ ์ž…๋ ฅํ•œ ๊ฐ’์ด ๋‹ค ์‚ฌ๋ผ์ ธ์„œ ๊ทธ๋Ÿฐ๊ฑฐ์˜€๋‹ค.
์ˆ˜์—… ์‹œ๊ฐ„์— prevent ๊ฑธ์–ด์ค˜์•ผ ํ•œ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ ๊ทธ์ƒˆ ๊นŒ๋จน์€ ^^;;; javascript ์ฝ”๋“œ ๋‹ค์‹œ ์ˆ˜์ •..

๐Ÿ”ฅ์ˆ˜์ • ์‚ฌํ•ญ

pwdBtn.addEventListener("click", function(e) {
    e.preventDefault();

profile

GiantStepDEV

@kongmi

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!