2012-01-12 2 views
0

은과 같이 텍스트 블록을 감안할 때 :텍스트 단락이 주어지면 처음부터 마지막 ​​공백까지를 어떻게 얻을 수 있습니까?

"Hello world this is http://yahoo.com " 

가 키를 누를 때 나는 링크에 대한 텍스트를 찾고 있어요. 나는 텍스트에 링크를 사용자가 아직 입력 상태를보고 싶지 않아 예 :

"Hello world this is http://yahoo.co" 

가 어떻게 텍스트 VAR의 모든 모든 방법 마지막 백색, 왼쪽, 0부터 시작 얻을 수 있습니다 공간, 나는 사용자가 그것을 입력하는 동안 URL을 얻지 못하는 것을 의미합니까?

감사 Hello world this is http://yahoo.com

Hello world this is

+0

사용자가 텍스트 입력을 마칠 때 링크를 찾기 위해 onblur 이벤트를 사용할 수 있습니다. –

+0

thx하지만 그 이유는 사용자가 입력하는 동안 필요합니다. –

+0

사용자가 텍스트 끝에 항상 공백을 입력하거나 실제로 "Hello world this http : //"라고 입력한다고 가정합니까? yahoo.com "<- no space – Archer

답변

1

두 가지 방법을 반환해야 Hello world this is http://yahoo.co를 입력하면 반환해야 Hello world this is http://yahoo.com를 입력하면 업데이트]

:

첫째, 정기적 애 썼는데 ression :

var str = "Hello world this is http://yahoo.co", 
    expr = /(.+\s).*$/ 
    result = str.match(expr) 
; 

console.log(result); 
// => ["Hello world this is http://yahoo.co", "Hello world this is "] 

console.log(result[1]); 
// => "Hello world this is " 

둘째, lastIndexOf는 :

var str  = "Hello world this is http://yahoo.co", 
    lastIdx = str.lastIndexOf(' ') 
    result = str.substr(0, lastIdx + 1) 
; 

console.log(result); 
// => "Hello world this is " 

후자는 공백으로 작동 이것은, 그러나, lastIndexOf로 정규 표현식을지지 않습니다.

+0

고마워요.하지만 "Hello world this is http://yahoo.com"이라고 입력하면 뒷 공간에 링크가 반환되지 않습니다. –

+0

감사합니다. 두 번째 시도했지만 여전히 작동하지 않습니다. "Hello World is yahoo.com"이라고 입력하면 .com 다음에 "Hello World this yahoo.com"이 반환됩니다. 권리? –

+0

볼 수있는 코드 만 디버깅 할 수 있습니다. 아마도 사용중인 전체 코드를 게시 하시겠습니까? 위 코드를'var str = "Hello World this yahoo.com"로 사용하면'Hello World this is yahoo.com'을 반환합니다. 그건 당신의 질문과 의견에 맞는 것 같습니다. –

관련 문제