最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - whats wrong with this reg ex - Stack Overflow

programmeradmin5浏览0评论

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 use location.pathname instead. – Gumbo Commented Nov 19, 2010 at 18:32
Add a ment  | 

11 Answers 11

Reset to default 5

Try 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]
发布评论

评论列表(0)

  1. 暂无评论