javascript - 这个 javascript 正则表达式会替换空格吗?

标签 javascript regex

我正在浏览 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/

相关文章:

javascript - subscribe() 后获取返回变量的值

javascript - Underscore 中的外部模板

c# - jQuery - 启用基于复选框的文本框 - 动态生成

java - 浏览器字段 Cookie 删除

regex - 使用正则表达式Dart/Flutter提取数据

ruby - 如何进行忽略控制字符的文本搜索?

javascript - 如何替换不在 id ="*"中的字符串?

javascript - 我们应该将 Material-UI 与 React 一起使用吗

php - 如何替换文本 URL 并排除 HTML 标记中的 URL?

arrays - Ruby 匹配数组中的字符串