์ ๊ท ํํ์(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();