admin管理员组文章数量:1401849
I have this reg ex
var id = url.match(/(\d+)\/\d+$/)[1];
that is parsing a url
It is grabbing the 48 as it should but it fails on
why is the # messing up the regex
I have this reg ex
var id = url.match(/(\d+)\/\d+$/)[1];
that is parsing a url
http://whatever./something/70/48/359
It is grabbing the 48 as it should but it fails on
http://whatever./something/70/48/359#
why is the # messing up the regex
Share Improve this question asked Nov 19, 2010 at 18:25 Matt ElhotibyMatt Elhotiby 44.1k91 gold badges224 silver badges328 bronze badges 2-
2
The second URL does not end with a digit but with
#
. – Gumbo Commented Nov 19, 2010 at 18:26 -
Where does
url
e from? If it’s the current document’s location, better uselocation.pathname
instead. – Gumbo Commented Nov 19, 2010 at 18:32
11 Answers
Reset to default 5Try this regular expression instead:
/(\d+)\/\d+(?:$|[?#])/
This matches not just the end of the string but also the delimiters for the query (?
) and the fragment (#
).
And if your url
happens to be the document’s location (e.g. location.href
), better use location.pathname
instead.
Because of the ...\d+$
which means "after the last number the end of the URL must follow immediately" (with no # inbetween).
The #
is not a digit. Try:
(\d+)\/\d+#?$
Because it is expecting the string to end with digits, because of the dollar sign in \d+$
.
Here's your regex fixed to ignore #anchors
and ?query=strings
:
var id = url.match(/(\d+)\/\d+(|#.*|\?.*)$/)[1];
The (|#.*|\?.*)
part will either match nothing, #anything or ?anything.
Or more efficiently:
var id = url.match(/(\d+)\/\d+([#\?].*)?$/)[1];
The $
at the end anchors the regex to the end of the URL, meaning it will only match if the two numbers e right at the end. You can add (#.*)?
to the end to allow for an optional #anchor
:
// Allow optional anchor.
var id = url.match(/(\d+)\/\d+(#.*)?$/)[1];
You may also want to allow a query string as well, the part after a question mark. For that add (\?.*?)?
as well:
// Allow optional query string and anchor.
var id = url.match(/(\d+)\/\d+(\?.*?)?(#.*)?$/)[1];
Because the \d+
and $
indicates that the line must end with one or more digits. As the second example ends with #
there's no match in that case.
The $ anchors your regexp to the end of the string. Since # is not a digit, the regexp won't match the second example.
The \d+$
part means "one or more digits, followed by the end of string". If there's a #
in between, it no longer matches.
If all you want to do is allow a final #
sign, try:
/(\d+)\/\d+\#?$/
This is not exactly a fix to the regex, but you can stop the "#" from showing up in the URL by changing the links that are <a href="#">
to be <a href="#" onclick="return false;">
It would probably be easier to do it unobtrusively with jQuery like this...
$(function() {
$("a[href=\\#]").click(function(e) {
e.preventDefault();
});
});
var id = url.match(/(\d+)\/\d+#?$/)[1];
Will fix ya
.match(/(\d+)\/\d+[^\/]*$/)[1]
本文标签: javascriptwhats wrong with this reg exStack Overflow
版权声明:本文标题:javascript - whats wrong with this reg ex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744318143a2600364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论