基本JQuery:用另一个替换链接字符串

时间:2011-06-23 18:54:15

标签: javascript jquery regex replace

如何更改:

http://test.dev/event?event=94&page=4

http://test.dev/contest?contest=94&page=4

使用Jquery?

我有以下代码,并希望在右键单击时更改它:(案例2)

// will_paginate + ajax 
// updates two divs: #pages and #pagelink
// see places#search controller; passes 4 parameters to the search action

$(function(){
  $('.pagination a').mousedown(function(event) {
      switch (event.which) {
          case 1:
              $(".pagination a").live("click", function(event) {
              event.preventDefault();

              // Javascript Regular expression function; extracts page number from full url
              var extractPageNumber = function(s) {
                var r=/page=(\d+)/, m=(""+s).match(r);
                return (m) ? Number(m[1]) : undefined;
              };
              var pageregex = extractPageNumber($(this).attr('href'));
              $(".pagination").html("Page is loading...")
              $.get("/event", {  event: $("input[name=event]").val(), page: pageregex }, function(data) {
                $('#pagelink').html(data.page_link);
                console.log(data.page_link);
                $('#tweets').html(data.html);
                });
              return false;
              });
          break;

          case 2:
                break;
    }
  });
});

2 个答案:

答案 0 :(得分:1)

这是你想要的吗?

...
case 2:
   var href = $(this).attr("href").replace(/event/g, "contest")
   // do stuff with href

答案 1 :(得分:0)

"http://test.dev/event?event=94&page=4".replace(/event/g,'contest');