728x90

String.prototype.split(separator, limit)

1."85@@86@@53".split("@@"); //['85', '86', '53'];
2."bannana".split(); //["banana"];
3."원,투,쓰리".split(",",2); // ["원","투"]

Array.prototype.join(separator)

배열을 스트링으로 변환 합니다.
1.["slugs","snails","puppy dog's tails"].join(' and '); //"slugs and snails and puppy dog's tails"
2.['Giants', 4, 'Rangers', 1].join(' '); //"Giants 4 Rangers 1"
3.[1962,1989,2002,2010].join(); //"1962,1989,2002,2010"

replaceAll

정규 표현식을 사용하지 않고 문자열 대체 수행 예제
1.String.prototype.replaceAll = function(find, replaceWith) {
2. return this.split(find).join(replaceWith);
3.}
4."the man and the plan".replaceAll('the','a'); //"a man and a plan"

repeat

1.String.prototype.repeat = function(times) {
2. return new Array(times+1).join(this);
3.}
4."go ".repeat(3) + "Giants!"; //"go go go Giants!"

사용 패턴

01.var getDomain = function(url) {
02. return url.split('/',3).join('/');
03.}
04.
09.
10.
11.var beheadMembers = function(arr, removeStr) {
12. var regex = RegExp("[,]?" + removeStr);
13. return arr.join().split(regex).slice(1);
14.}
15.
16.//make an array containing only the numeric portion of flight numbers
17.beheadMembers(["ba015","ba129","ba130"],"ba"); //["015","129","130"]
18.
19.var beheadMembers = function(arr, removeStr) {
20. var regex = RegExp("[,]?" + removeStr);
21. var result = arr.join().split(regex);
22. return result[0] && result || result.slice(1); //IE workaround
23.}

패턴 매치

1.var results = ['sunil','23:09','bob','22:09','carlos','mary','22:59'];
2.var badData = results.join(',').match(/[a-zA-Z]+,[a-zA-Z]+/);
3.badData; //["carlos,mary"]
01.//test 1 - using join/split
02.var arr = [], x = 1000;
03.while (x--) {arr.push("ba" + x);}
04.
05.var beheadMembers = function(arr, regex) {
06. return arr.join().split(regex).slice(1);
07.}
08.
09.var regex = RegExp("[,]?" + 'ba');
10.var timer = +new Date, y = 1000;
11.while(y--) {beheadMembers(arr,regex);};
12.+new Date - timer;
13.
14.//FF 3.6 733ms
15.//Ch 7 464ms
16.//Sa 5 701ms
17.//IE 8 1256ms
18.
19.//test 2 - using native map function
20./*
21.var arr = [], x = 1000;
22.while (x--) {arr.push("ba" + x);}
23.
24.var timer = +new Date, y = 1000;
25.while(y--) {
26. arr.map(function(e) {
27. return e.replace('ba','')
28. });
29.}
30.+new Date - timer;
31.*/
32.//FF 3.6 2051ms
33.//Cr 7 732ms
34.//Sf 5 1520ms
35.//IE 8 (Not supported)
728x90

'WEB' 카테고리의 다른 글

replaceall 정규식  (0) 2012.09.26
childnodes 문제  (0) 2012.09.26
setAttribute class 안먹을 때  (0) 2012.09.22
html 스크롤바  (0) 2012.09.06
line-height default value  (0) 2012.09.05

+ Recent posts