2013-02-11 2 views
2

이것이 두 번 게시물 인 경우 용서해주세요. 그러나 나를 위해 일한 비슷한 것을 찾지 못했습니다.jQuery 스트립 첫 번째 기간

나는 도메인 이름이 예입니다. google.co.uk (하지만 google.com 일 수도 있음).

사람이 수 ... 'google.co.uk'.split(/.(.+)?/)[1]; 그러나 그것은 작동하지 않는 것 :이 발견 antoher 게시물에 ["google", "co.uk"] 또는 ["google", "com"]

: 나는 배열이 같은 개체를 얻을 수 있도록 첫 번째 기간에이를 분할해야 도와주세요? 미리 감사드립니다!

답변

6

첫 번째 대체하십시오. 문자열과 같이 (|와 같이) 나타나지 않고 그 문자를 분리하는 다른 것과 함께.

str.replace 만이 기본적으로 발견되는 문자의 첫 번째 인스턴스를 대체하는 바와 같이, 코드는 매우 간단하다 : 당신은이 모든 것을 만들고있어

str = "google.co.uk"; 
str.replace(".", "|").split("|"); 
+0

아, 그렇게 간단하지 않은 이유는 무엇입니까?! 고마워요! –

0

jsFiddle

var text = 'google.co.uk'; 

//Returns the position of the first '.' 
var index = text.indexOf('.'); 

//Grab the the start of the string up to the position of the '.' 
var first = text.substring(0, index); 

//Start from the '.' + 1(to exclude the '.') and go to the end of the string 
var second = text.substring(index + 1); 

alert(first); //google 
alert(second); //co.uk 
+0

-1 개선 및 코드 작동에 대한 설명이 없습니까? 이상한... –

0

조금 복잡해. ...

string='google.com'; 
dot=string.indexOf('.',0); 


name=string.substring(0,dot); 
domain=string.substring(dot+1,string.length); 

arr= new Array(name, domain); 
alert(arr); 

롤, 이미 더 나은 솔루션을 참조 ...

var domain = 'google.co.uk'; 
alert(domain.split(/^(.+?)\./ig).splice(1)); 
0

당신은 몇 가지 기본 자바 스크립트 기능을 사용할 수 있습니다 :) 너무 유사한 솔루션 ...

: 그것은 쉬운 하나 개의 라인에서 가능
관련 문제