2012-04-18 10 views
0

私はTwitterのブートストラップのコードを閲覧していますが、このスニペットを何度か繰り返しています。このjavascriptの正規表現は空白を置き換えますか?

href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7 

正規表現は私の盲点であり、私はそれらのほとんどを理解することができません。それは何を置き換えるのですか?それは#の後に空白ですか?

サイドノート。誰もが正規表現の授業のための良いソースをお勧めしますか?

+3

サイド回答します。http://www.regular-expressions。 info/ – musefan

答えて

6

は、ここでそれが探しているものです:

.*  # any number of characters (optional) 
(?= # open a lookahead 
#  # find a hash tag 
[^\s]+ # at least one non-whitespace character 
$  # end of line 
)  # close the lookahead 

ので、例えば、それはハッシュタグの前に来るものと一致します。

replace this!#foobar <-- matches: replace this! 
hello world#goodbye <-- matches: hello world 
no match here !  <-- doesn't match anything because there is no hash 
what?#     <-- does not match because there is nothing after the hash 
what?# asd    <-- does not match because there is a whitespace-character 
+0

有効な一致と無効な一致のいくつかの例は、その答えにうまくいくでしょう;) – musefan

+0

@musefan良いアイデア。私はいくつかを投げます。 – alan

+1

注 '+'は1つ以上と一致します – Christoph

関連する問題