0

我正在浏览 twitter bootstrap 的代码,我已经在这个片段中昏迷了几次。

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

正则表达式是我的盲点,我无法弄清楚其中的大部分。它在取代什么?#后面是空格吗?

边注。任何人都可以推荐一个好的正则表达式学费来源吗?

4

1 回答 1

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
于 2012-04-18T14:07:05.347 回答