리자

[펌] 정규식 함수

· 14년 전 · 1776
함수 코드예제 코드설명
Array RegExp.exec (to be checked) var myRe=/d(b+)(d)/ig;
var myArray = myRe.exec("cdbBdbsbz");
/d(b+)(d)/ig

cdbBdbsbzⓥ

myArray.index =1 ; (처음으로 매칭되는 위치, 컴터가 늘 그렇듯 위치는 0번째부터 센다.)
myArray.input = cdbBdbsbz; (체크할 대상)
myArray[0] = dbBd;(검사에 통과한 부분)
myArray[1] = bB;(1번째 괄호에서 체크된 부분)
myArray[2] = d;(2번째 괄호에서 체크된 부분)

myRe.lastIndex =5 ; (다음번 체크를 하기위한 위치.)
myRe.ignoreCase = true; (/i 플래그 체크)
myRe.global = true; (/g 플래그 체크)
myRe.multiline = false; (/m 플래그 체크)

RegExp.$_ = cdbBdbsbz;(입력한 스트링)
RegExp.$1 = bB;(1번째 괄호에서 체크된 부분 )

boolean RegExp.test(to be checked) var myRe=/d(b+)(d)/ig;
var checked = myRe.test("cdbBdbsbz");
document.write("checked = " + checked +";<br>");
/d(b+)(d)/ig

cdbBdbsbzⓥ

실행결과: checked = true;

String RegExp.toString() var myRe=/d(b+)(d)/ig;
var str = myRe.toString();
document.write(str);
실행 결과: /d(b+)(d)/ig
String String.replace(pattern or string, to be replaced) var str = "abcdefe";
document.write(str.replace("e" , "f")); 실행 결과: abcdffe

e가 2번 있지만, 첫번째 인자가 정규식이 아니라 문자열일 경우는 첫번째 것만 바꾼다.
var str = "aba";
document.write(str.replace(/^a/ , "c")); 실행 결과: cba
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
newstr = str.replace(re, "$2, $1");
document.write(newstr) 실행 결과: Smith, John

re에 의해서 찾아진 문자열 들은 re에서 ()로 표현된 순서대로 $1, $2와 같이 변수로 저장된다.
var re = /\s(?:http|https):\/\/\S*(?:\s|$)/g;
var str = "url is http://iilii.egloos.com/ !!\n";
str += "blah home: http://www.blah.co.kr";
newstr = str.replace(re, function (str,p1,offset,s) {
return "<a href='" + str + "'>" + str + "</a>";
}
).replace(/\n/, "<br>");
document.write(newstr); url is http://iilii.egloos.com/ !!
blah home: http://www.blah.co.kr

str: 찾은 문자열
p1: ()에서 검색된 1번째 문자열. 마찬가지로 p2,p3 등도 가능
offset: str을 찾은 위치
s : 원본 문자열.

Array String.match(regular expression var str = "ABCdEFgHiJKL";
var myResult = str.match(/[a-z]/g );
for(var cnt = 0 ; cnt < myResult.length; cnt++){
document.write(cnt +":" + myResult[cnt] +"<br>");
}

document.write("비교<br>");

var str = "ABCdEFgHiJKL";
var myResult = /[a-z]/g.exec(str);
for(var cnt = 0 ; cnt < myResult.length; cnt++){
document.write(cnt +":" + myResult[cnt] +"<br>");
} 실행 결과:
0:d
1:g
2:i
비교
0:d

String.match(RegExp) =>g flag가 있어도 다 찾아낸다.
RegExp.exec(String) =>g flag가 있으면, 한 개만 찾고 끝낸다.
Array String.split([separator[, limit]]) var str = "ABCdEFgHiJKL";
var myResult = str.split(/[a-z]/g , 3);
for(var cnt = 0 ; cnt < myResult.length; cnt++){
document.write(cnt +":" + myResult[cnt] +"<br>");
} 실행 결과:
0:ABC
1:EF
2:H

주어진 문자열을 separator를 기준으로 limit 만큼 자른다.<div class='small'>[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]</div>
|
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
14년 전 조회 2,763
14년 전 조회 872
14년 전 조회 1,129
14년 전 조회 1,990
14년 전 조회 2,390
14년 전 조회 1,449
14년 전 조회 1,570
14년 전 조회 909
14년 전 조회 1,234
14년 전 조회 1,237
14년 전 조회 1,984
14년 전 조회 1,259
14년 전 조회 932
14년 전 조회 1,030
14년 전 조회 335
14년 전 조회 1,083
14년 전 조회 994
14년 전 조회 1,013
14년 전 조회 1,555
14년 전 조회 2,425
14년 전 조회 1,475
14년 전 조회 1,264
14년 전 조회 1,637
14년 전 조회 891
14년 전 조회 887
14년 전 조회 1,165
14년 전 조회 1,369
14년 전 조회 1,001
14년 전 조회 1,299
14년 전 조회 1,913
14년 전 조회 894
14년 전 조회 1,233
14년 전 조회 1,247
14년 전 조회 962
14년 전 조회 982
14년 전 조회 1,344
14년 전 조회 1,841
14년 전 조회 1,743
14년 전 조회 1,652
14년 전 조회 1,023
14년 전 조회 1,219
14년 전 조회 1,052
14년 전 조회 949
14년 전 조회 2,134
14년 전 조회 1,401
14년 전 조회 1,355
14년 전 조회 1,412
14년 전 조회 1,621
14년 전 조회 992
14년 전 조회 1,777
14년 전 조회 4,057
14년 전 조회 1,995
14년 전 조회 2,092
14년 전 조회 1,504
14년 전 조회 1,986
14년 전 조회 1,622
14년 전 조회 1,425
14년 전 조회 1,112
14년 전 조회 1,075
14년 전 조회 1,284
14년 전 조회 1,524
14년 전 조회 6,063
14년 전 조회 1,584
14년 전 조회 1,302
14년 전 조회 1,363
14년 전 조회 935
14년 전 조회 1,124
14년 전 조회 1,725
14년 전 조회 1,197
14년 전 조회 858
14년 전 조회 1,388
14년 전 조회 1,462
14년 전 조회 1,791
14년 전 조회 1,109
14년 전 조회 4,666
14년 전 조회 1,264
14년 전 조회 1,239
14년 전 조회 1,259
14년 전 조회 1,460
14년 전 조회 1,272
14년 전 조회 1,004
14년 전 조회 1,073
14년 전 조회 1,507
14년 전 조회 811
14년 전 조회 1,399
14년 전 조회 1,391
14년 전 조회 1,472
14년 전 조회 4,217
14년 전 조회 1,444
14년 전 조회 1,151
14년 전 조회 1,579
14년 전 조회 1,749
14년 전 조회 956
14년 전 조회 2,197
14년 전 조회 2,171
14년 전 조회 1,748
14년 전 조회 1,717
14년 전 조회 1,225
14년 전 조회 1,705
14년 전 조회 1,631