자바로 도메인만 추출 할려고 합니다. 정보
자바로 도메인만 추출 할려고 합니다.
본문
현제 임시로 사용하고 있는 스크립트 입니다
// 도메인검사' 검사
function wrestCASTadd(fld){
if (!wrestTrim(fld))
return;
var pattern = /(^[a-zA-Z0-9\.]+$)/;
if (!pattern.test(fld.value)){
if (wrestFld == null){
wrestMsg = wrestItemname(fld) + " : 도메인이 올바르지 않습니다.\n\n http:// 은 넣지 마세요";
wrestFld = fld;
}
}
}
위와 같이 . 과 영문만 허락하는 것으로 대처 했습니다만...
만약 http://abc.com/mm.php 를 넣으면 자동으로 abc.com 만 추출하고 나머지는 잘라버릴 수 없을까요?
// 도메인검사' 검사
function wrestCASTadd(fld){
if (!wrestTrim(fld))
return;
var pattern = /(^[a-zA-Z0-9\.]+$)/;
if (!pattern.test(fld.value)){
if (wrestFld == null){
wrestMsg = wrestItemname(fld) + " : 도메인이 올바르지 않습니다.\n\n http:// 은 넣지 마세요";
wrestFld = fld;
}
}
}
위와 같이 . 과 영문만 허락하는 것으로 대처 했습니다만...
만약 http://abc.com/mm.php 를 넣으면 자동으로 abc.com 만 추출하고 나머지는 잘라버릴 수 없을까요?
댓글 전체

var str="http://abc.com/mm.php";
//var str="abc.com/mm.php";
str = str.split("/", 3);
if (str[0] == "http:") {str = str[2]; }
else { str = str[0]; }
이렇게하시면 str의 값이 http://abc.com/mm.php 이든
abc.com/mm.php 이든 abc.com 만 뽑아줍니다.
다만 http:/abc.com 이나 htt://abc.com 등 오타가 나면
쌩뚱맞은걸 뽑아주겠죠 ㅜㅜ
//var str="abc.com/mm.php";
str = str.split("/", 3);
if (str[0] == "http:") {str = str[2]; }
else { str = str[0]; }
이렇게하시면 str의 값이 http://abc.com/mm.php 이든
abc.com/mm.php 이든 abc.com 만 뽑아줍니다.
다만 http:/abc.com 이나 htt://abc.com 등 오타가 나면
쌩뚱맞은걸 뽑아주겠죠 ㅜㅜ
<script>
var patt= /(http:\/\/)?([^/]+)\/?(.*)$/ig;
var str = "naver.com/main.php";
document.write(str.replace(patt, '$2'));
document.write("<br>");
var str = "http://naver.com/main.php";
document.write(str.replace(patt, '$2'));
document.write("<br>");
var str = "http://naver.com";
document.write(str.replace(patt, '$2'));
</script>
테스트 한겁니다.
var patt= /(http:\/\/)?([^/]+)\/?(.*)$/ig;
var str = "naver.com/main.php";
document.write(str.replace(patt, '$2'));
document.write("<br>");
var str = "http://naver.com/main.php";
document.write(str.replace(patt, '$2'));
document.write("<br>");
var str = "http://naver.com";
document.write(str.replace(patt, '$2'));
</script>
테스트 한겁니다.

흐 정규식이 있었네요.. 전 왜 split 을 먼저 떠올렸을까요 ㅋㅋ
윗님 쓴글 보고 잠시나마.
오타는 답안나오는군요.
님하곤 약간 답이 엇갈리는 군요 ㅋㅋ;;
이래 저래.. 오타는 답 안나오는군요.
첨에는
str.strtolower().replace('http://', '').split('/')[0] 생각했다가.... 그넘의 http:// 넘 답안나오네요.
오타는 답안나오는군요.
님하곤 약간 답이 엇갈리는 군요 ㅋㅋ;;
이래 저래.. 오타는 답 안나오는군요.
첨에는
str.strtolower().replace('http://', '').split('/')[0] 생각했다가.... 그넘의 http:// 넘 답안나오네요.

두분 감사 드립니다.
어째든 두가지다 완벽하지는 않는거지요?
간단한 코드부터 다 적용해볼께요~
감사 드리구요~
어째든 두가지다 완벽하지는 않는거지요?
간단한 코드부터 다 적용해볼께요~
감사 드리구요~

실제 적용하기에 쬐금 애먹었네요
근데 네발가락님 문법은 제가 해석이 않되네요,,,ㅎㅎ
근데 네발가락님 문법은 제가 해석이 않되네요,,,ㅎㅎ
흠냐 ;ㅋㅋ