2011-04-20 4 views
0

최근 사용자가 내 트윗을 표시 할 수있는 작은 앱을 만들었습니다. jQuery 또는 Javascript를 사용하여 해시 태그 및 http : //를 검색하거나 찾는 방법이 있는지 궁금합니다.jQuery를 사용하여 #을 어떻게 찾습니까?

문장의 간단한 예는 다음과 같을 수 있습니다

The quick #brown fox jumps over the #lazy dog 

답변

4

twitter-text-js을 시도해 볼 수 있습니다. auto link @mentions, #hashtags 및 url과 같은 작업을 수행 할 수 있습니다.

[{ 
    hashtag: "hashtag", 
    indices: [ 16, 24 ] 
}] 
2
+0

덕분에 너무 많은,하지만 실행시 :

twttr.txt.extractHashtagsWithIndices('A @screen_name, #hashtag, and url: http://twitter.com autolinked') 

가 발생합니다 : # 해시 태그의

"A @<a class="tweet-url username" data-screen-name="screen_name" href="http://twitter.com/screen_name" rel="nofollow">screen_name</a>, <a href="http://twitter.com/search?q=%23hashtag" title="#hashtag" class="tweet-url hashtag" rel="nofollow">#hashtag</a>, and url: <a href="http://twitter.com" rel="nofollow" >http://twitter.com</a> autolinked" 

또는 찾는 인덱스 :

twttr.txt.autoLink('A @screen_name, #hashtag, and url: http://twitter.com autolinked') 

가 발생합니다 그것은 jQuery를 통해서만 첫 번째 픽업을한다. e는 "문자열"에 더 많은 해시 태그가있는 이유는 무엇입니까? – ApPeL

+0

모든 항목을 얻으려면 exec를 여러 번 호출해야합니다. –

2

자바 스크립트의에서 작업을 참조 문자열

hashtags = /\#\w+/g.exec("The quick #brown fox"); 

의 일부와 일치하는 정규 표현식을 사용할 수 있습니다. search() 메서드는 문자가 나오는 문자열에서 색인을 제공합니다.

<p id="mystring">Hello, how are #you today?</p> 

alert($("#mystring").html().search("#")); 
+1

나는 downvote가 설명을 필요로한다고 생각한다. 나는 그의 코드를 밖으로 시도하고 완벽하게 작동합니다. – clamchoda

+0

동의. downvote에 대한 설명을 제공해주십시오. –

관련 문제