공백/태그제거, 단어찾기/바꾸기, 문자열->배열 변환 함수등
페이지 정보
작성자 서방님 댓글 0건 조회 102회 작성일 06-09-12 17:51본문
<script language="JavaScript">
<!--
// HTML 특수문자를 변환
String.prototype.htmlChars = function() {
var str = ((this.replace('"', '&')).replace('"', '"')).replace(''', ''');
return (str.replace('<', '<')).replace('>', '>');
}
//좌우 공백없애는 함수
String.prototype.trim = function(){
return this.replace(/(^s*)|(s*$)/g, "");
// 사용방법 : form.msg.value.trim();
}
//좌우 공백없애는 함수
function trim(string) {
var temp = "";
string = '' + string;
splitstring = string.split(" ");
for(i = 0; i < splitstring.length; i++)
temp += splitstring[i];
return temp;
// 사용방법 : trim(form.msg.value);
}
// 왼쪽 공백없애는 함수
String.prototype.ltrim = function() { return this.replace(/^s*/g, ""); }
// 오른쪽 공백없애는 함수
String.prototype.rtrim = function() { return this.replace(/s*$/g, ""); }
// 태그만 제거
String.prototype.stripTags = function() {
var str = this;
var pos1 = str.indexOf('<');
if (pos1 == -1) return str;
else {
var pos2 = str.indexOf('>', pos1);
if (pos2 == -1) return str;
return (str.substr(0, pos1) + str.substr(pos2+1)).stripTags();
}
}
// 대소문자 구별하지 않고 단어 위치 찾기
String.prototype.ipos = function(needle, offset) {
var offset = (typeof offset == "number")?offset:0;
return str.toLowerCase().indexOf(needle.toLowerCase(), offset);
}
// 대소문자 구별하지 않고 뒤에서부터 단어위치 찾기
String.prototype.ripos = function(needle, offset) {
var offset = (typeof offset == "number")?offset:0;
return str.toLowerCase().lastIndexOf(needle.toLowerCase(), offset);
}
//특정문자열을 모두 찾아서 원하는 문자열로 모두 바꾸기(대소문자구분함)
String.prototype.replaceExp = function(from, to){
return this.replace(new RegExp(from, "g"), to);
}
String.prototype.replaceEval = function(from, to){
return this.replace(eval("/" + from + "/g"), to);
}
//특정문자열을 모두 찾아서 원하는 문자열로 모두 바꾸기 (대/소문자 구분안함)
String.prototype.replaceAll = function(from, to)
{
var returnStr = this;
var tempStr = this.toLowerCase();
var fromStr = from.toLowerCase();
var count = fromStr.length;
while( tempStr.indexOf( fromStr ) >= 0 )
{
var word = returnStr.substr( tempStr.indexOf( fromStr ), count );
tempStr = tempStr.replace( from, to );
returnStr = returnStr.replace( word, to );
}
return returnStr;
}
// 문자열을 배열로
String.prototype.toArray = function() {
var len = this.length;
var arr = new Array;
for (var i=0; i<len; i++) arr[i] = this.charAt(i);
return arr;
}
댓글목록
등록된 댓글이 없습니다.