我正在浏览 twitter bootstrap 的代码,我在这段代码中陷入了几次昏迷。
href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
正则表达式是我的盲点,我无法弄清楚其中的大多数。它正在取代什么? #后面是空格吗?
旁注。谁能推荐一个好的正则表达式教程资源?
最佳答案
这是它正在寻找的内容:
.* # 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
关于javascript - 这个 javascript 正则表达式会替换空格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10211037/