특강_JavaScript 정규 표현식.pdf
const regex=/abc/;
document.write(regex);
document.write("<br>");
const regex2=new RegExp("abc");
document.write(regex2);
document.write("<br>");
const regex3=new RegExp(/abc/);
document.write(regex3);
document.write("<br>");
const regex4=/apple/;
const regex5=/apple2/;
const regex6=new RegExp("banana2");
const regex7=new RegExp(/banana/);
//apple이란 단어가 있으면 참, 아니면 거짓
var a1= regex4.test("Hello banana and apple hahaha")
var a2= regex5.test("Hello banana and apple hahaha")
var a3= regex6.test("Hello banana and apple hahaha")
var a4= regex7.test("Hello banana and apple hahaha")
document.write(a1);
document.write("<br>");
document.write(a2);
document.write("<br>");
document.write(a3);
document.write("<br>");
document.write(a4);
document.write("<br>");
const regex11=/apple/;
const txt = "Hello banana and apple hahaha";
var a11=txt.match(regex11);
document.write("@# a11=>"+a11);
document.write("<br>");
var a12=txt.match(/apple/);
document.write("@# a12=>"+a12);
document.write("<br>");
//g 모든 패턴을 검색
var a13=txt.match(/apple/g);
document.write("@# a13=>"+a13);
document.write("<br>");
// txt안에 있는 apple이 watermelon으로 변경
let a14=txt.replace(regex11,"watermelon");
document.write("@# a14=>"+a14);
document.write("<br>");
//줄바꿈이 포함된 3줄 문자열(console.log에만 표시됨)
const str="Hello World and Power Hello?\\n Power Overwhelming!!"
document.write("@# a15=>"+str);
console.log("@# a15=>"+str);
document.write("<br>")
const str2="Hello World and Power Hello? Power Overwhelming!!"
document.write("@# str2=>"+str2);
document.write("<br>")
// let a15 = str.match(/Hello/);
// let a15 = str.match(/World/);
// let a15 = str.match(/^World/); //null
let a15 = str.match(/^Hello/); //^는 문장의 시작점을 의미
document.write("@# a15=>"+a15);
document.write("<br>");
const str3="abcABC"
let a16 = str3.match(/a/);
//gi: 모든 패턴을 대소문자 가리지 않고 검색
// let a16 = str3.match(/a/gi);
document.write("@# a16=>"+a16);
document.write("<br>")
let a17="kokokoko".match(/ko/);
document.write("@# a17=>"+a17);
document.write("<br>")
// let a18="kokokoko".match(/ko+/); //ko
// "o"만 +를 적용(o가 여러개)
let a18="kooookoooo".match(/ko+/); //koooo
document.write("@# a18=>"+a18);
document.write("<br>")
// kokoko,ko: ko를 그룹화해서 찾음(정규식 그룹화)
let a19="kokokoko".match(/(ko)+/); //ko
document.write("@# a19=>"+a19);
document.write("<br>")
// ko,ko: ko를 그룹화해서 첫번째 찾음(정규식 그룹화)
let a20="kooookoooo".match(/(ko)+/); //ko
document.write("@# a20=>"+a20);
document.write("<br>");

const regex=/abc/;
document.write(regex);
document.write("<br>");
const regex2=new RegExp("abc");
document.write(regex2);
document.write("<br>");
const regex3=new RegExp(/abc/);
document.write(regex3);
document.write("<br>");
const regex4=/apple/;
const regex5=/apple2/;
const regex6=new RegExp("banana2");
const regex7=new RegExp(/banana/);
//apple이란 단어가 있으면 참, 아니면 거짓
var a1= regex4.test("Hello banana and apple hahaha")
var a2= regex5.test("Hello banana and apple hahaha")
var a3= regex6.test("Hello banana and apple hahaha")
var a4= regex7.test("Hello banana and apple hahaha")
document.write(a1);
document.write("<br>");
document.write(a2);
document.write("<br>");
document.write(a3);
document.write("<br>");
document.write(a4);
document.write("<br>");
const regex11=/apple/;
const txt = "Hello banana and apple hahaha";
var a11=txt.match(regex11);
document.write("@# a11=>"+a11);
document.write("<br>");
var a12=txt.match(/apple/);
document.write("@# a12=>"+a12);
document.write("<br>");
//g 모든 패턴을 검색
var a13=txt.match(/apple/g);
document.write("@# a13=>"+a13);
document.write("<br>");
// txt안에 있는 apple이 watermelon으로 변경
let a14=txt.replace(regex11,"watermelon");
document.write("@# a14=>"+a14);
document.write("<br>");
//줄바꿈이 포함된 3줄 문자열(console.log에만 표시됨)
const str="Hello World and Power Hello?\\n Power Overwhelming!!"
document.write("@# a15=>"+str);
console.log("@# a15=>"+str);
document.write("<br>")
const str2="Hello World and Power Hello? Power Overwhelming!!"
document.write("@# str2=>"+str2);
document.write("<br>")
// let a15 = str.match(/Hello/);
// let a15 = str.match(/World/);
// let a15 = str.match(/^World/); //null
let a15 = str.match(/^Hello/); //^는 문장의 시작점을 의미
document.write("@# a15=>"+a15);
document.write("<br>");
const str3="abcABC"
let a16 = str3.match(/a/);
//gi: 모든 패턴을 대소문자 가리지 않고 검색
// let a16 = str3.match(/a/gi);
document.write("@# a16=>"+a16);
document.write("<br>")
let a17="kokokoko".match(/ko/);
document.write("@# a17=>"+a17);
document.write("<br>")
// let a18="kokokoko".match(/ko+/); //ko
// "o"만 +를 적용(o가 여러개)
let a18="kooookoooo".match(/ko+/); //koooo
document.write("@# a18=>"+a18);
document.write("<br>")
// kokoko,ko: ko를 그룹화해서 찾음(정규식 그룹화)
let a19="kokokoko".match(/(ko)+/); //ko
document.write("@# a19=>"+a19);
document.write("<br>")
// ko,ko: ko를 그룹화해서 첫번째 찾음(정규식 그룹화)
let a20="kooookoooo".match(/(ko)+/); //ko
document.write("@# a20=>"+a20);
document.write("<br>")
const str4="그래픽 카드 a급 제품 재고량은 10개 입니다.011-8821-1예0로 연락주시길 바랍니다. Thank you ~!";
document.write("@# str4=>"+str4);
document.write("<br>")
// 첫번째 다를 찾음
let a21=str4.match(/다/);
document.write("@#21=>"+a21);
document.write("<br>")
// 2개의 다를 찾음
let a22=str4.match(/다/g);
document.write("@#22=>"+a22);
document.write("<br>")
let a23=str4.match(/그래픽 카드/g);
document.write("@#23=>"+a23);
document.write("<br>")
// 모든 a,0,g,드를 다 찾음
let a24=str4.match(/[a0g드]/g);
document.write("@#24=>"+a24);
document.write("<br>")
let a25=str4.match(/[0-9]/g);
document.write("@#25=>"+a25);
document.write("<br>")
// 숫자가 아닌 것을 모두 찾음.부정.
let a26=str4.match(/[^0-9]/g);
document.write("@#26=>"+a26);
document.write("<br>")
// 영어 알파벳 대문자/소문자를 모두 찾는다.
let a27=str4.match(/[a-zA-Z]/g);
document.write("@#27=>"+a27);
document.write("<br>")
// 한글, 영문, 숫자, 공백 모두 포함
let a28=str4.match(/[가-핳\\w\\s]/g);
document.write("@#28=>"+a28);
document.write("<br>")
// 한글, 영문, 숫자, 공백을 제외한 특수문자
let a29=str4.match(/[^가-핳\\w\\s]/g);
document.write("@#29=>"+a29);
document.write("<br>")

const str7="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 123456789";
// let a51 = str7.match(/[1-9]/g); //숫자만
// let a51 = str7.match(/[a-z]/g); //소문자만
// let a51 = str7.match(/[A-Z]/g); //대문자만
// let a51 = str7.match(/[A-z]/g); //대소문자 전부
let a51 = str7.match(/[^x-z]/g); //x,y,z제외
document.write("@# a51=>"+a51);
document.write("<br>");
const str8="Sunday Monday MonMonDay Tuesday Wednesday Thursday Friday Saturday";
// Mon모두 출력
// let a52 = str8.match(/(Mon)/g);
// (|): or
// let a52 = str8.match(/(Mon|Tues)/g);
// let a52 = str8.match(/(Mon|Tues)day/g);
// Mon이 있거나 없는 경우
// *: *앞에 입력된 문자가 있거나 없을 경우
// let a52 = str8.match(/(Mon)*day/g);
// +: +앞에 입력된 문자가 하나에서 여러개(최소 하나)
// let a52 = str8.match(/(Mon)+day/g);
// ?: ?앞에 입력된 문자가 없거나 하나인 경우(최대 하나))
let a52 = str8.match(/(Mon)?day/g);
document.write("@# a52=>"+a52);
document.write("<br>");

const str9="Sing Song when I'mttalking home i g";
// 3개 이상 알파벳을 3개로 자름
// {}:숫자를 단위로 표현하여 선택
// 3개 이상의 알파벳
// let a53 = str9.match(/[a-z]{3}/g);
// {n,}뒤의 인자를 비워 두면 n개 이상을 의미
// let a53 = str9.match(/[a-z]{3,}/g);
// 1개 이상 3개 이하 알파벳
// {n,m}: 앞에 입력된 문자가 n개 이상 m개 이하
// let a53 = str9.match(/[a-z]{1,3}/g);
// let a53 = str9.match(/[a-z]{1,3}/g);
// let a53 = str9.match(/[al]{1,2}/g);
// let a53 = str9.match(/[ng]{1,2}/g);
// \\w 모든 문자[A-z0-9]와 _언더바 까지 포함
// let a53 = str9.match(/\\w/g);
//\\w와 정반대 의미 공백문자만
let a53 = str9.match(/\\W/g);
document.write("@# a53=>"+a53);
document.write("<br>");
const str10="DingZong 12345696";
// let a54=str10.match(/\\d/g); //숫자만
let a54=str10.match(/\\D/g); //\\d랑 반대, 알파벳만
document.write("@# a54=>"+a54);
document.write("<br>");
정규식 실무 예제
//====== 1. 특정 단어로 끝나는지 검사=====
const txt="index.html";
let a61=txt.match(/html$/g); //html
document.write("@# a61=>"+a61);
document.write("<br>");
//====== 2. 숫자로만 이루어져 있는지 검사, 숫자말고 다른게 있으면 null=====
const txt2="12345";
// const txt2="12345a"; //null
let a62=txt2.match(/^\\d+$/g);
document.write("@# a62=>"+a62);
document.write("<br>");
//====== 3. 아이디 사용 검사 =====
// 알파벳 대소문자 또는 숫자로 시작하고 끝나며 4~10자리인지 검사
// const txt3="abc1";
const txt3="abc"; //null
let a63 = txt3.match(/^[A-Za-z0-9]{4,10}$/g);
document.write("@# a63=>"+a63);
document.write("<br>");
// ===== 4. 핸드폰 번호 형식 =====
// 3자리 + 하이픈(-) + 3,4자리+ 하이픈(-) + 4자리
const txt4="010-1234-5678";
// const txt4="010-1234-56782"; //끝자리가 5자리라 null
// const txt4="010-34-5678"; //중간자리가 2자리라 null
let a64 = txt4.match(/^\\d{3}-\\d{3,4}-\\d{4}$/g);
document.write("@# a64=>"+a64);
document.write("<br>");
//====== 5. 웹사이트 주소 형식 ======
// <http://나> <https://로> 시작하고, 알파벳,하이픈, dot으로 이루어져 있는 정규식
const txt5="<http://naber.com> 010-23231-21424 02-333-5555 [email protected] <https://google.com>";
let a65 = txt5.match(/https?:\\/\\/[\\w\\=\\.]+/g);
document.write("@# a65=>"+a65);
document.write("<br>");
// ===== 6. 전화번호 형식 =====
const txt6="<http://dogumaster> 010-1111-2222 02-333-7777 [email protected] <http://google.com>"
let a66 = txt6.match(/\\d{2,3}-\\d{3,4}-\\d{3,4}/g);
document.write("@# a66=>" +a66);
document.write("<br>");
// ===== 7. 이메일 형식 =====
let a67 = txt6.match(/[\\w\\-\\.]+\\@[\\w\\-\\.]+/g);
document.write("@# a67=>" +a67);
document.write("<br>");
// ===== 8. 특수기호 =====
const txt8="abCD$94#높*ㅍ"
let a68 = txt8.match(/[^a-zA-Z0-9가-핳ㄱ-ㅎ]/g);
document.write("@# a68=>" +a68);
document.write("<br>");