简体   繁体   English

此javascript正则表达式会替换空白吗?

[英]Does this javascript regex replace whitespace?

I'm browsing through the code for twitter bootstrap and I've coma across this snippet a few times. 我正在浏览Twitter引导程序的代码,并且在此代码段中出现了几次昏迷。

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

Regexes are my blindspot and I can't figure out most of them. 正则表达式是我的盲点,我无法弄清它们中的大多数。 What is it replacing? 替代什么? Is it whitespace after the #? #后面是否有空格?

Side note. 边注。 Can anyone recommend a good source for regex tuition? 谁能推荐正则表达式学费的好来源?

Here's what it's looking for: 它在寻找什么:

.*     # 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

So, for example, it matches what comes before a hash tag: 因此,例如,它与哈希标签之前的内容匹配:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM