我正在採取正則表達式的第一步。我想增加一個url字符串中的最後一位數字,但由於某種原因,我不能找出我增加其他數字。使用JavaScript和正則表達式的URL操作
例
字符串:http://example.com/18-something-something/6
應該變成:http://example.com/19-something-something/7
正如你所看到的,18
轉向19
這是林試圖避免:http://example.com/18-something-something/7
。
這是我的JS:
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
jQuery(document).ready(function() {
url = window.location.href;
var newrl = url.replace(/(\d+)+/g, function(match, number) {
return parseInt(number) + 1;
});
var m = url.match(/\/([^\/]+)[\/]?$/);
link = jQuery('a[class=nextpostslink]').attr('href');
if (!isNumeric(m[1])) {
jQuery('.post-content').find('img:first').wrap(jQuery("<div class='slideshow-wrapper'><a href=2>").attr("href", link));
jQuery('.post-content').find('img:first').after('<div id="start-slideshow"><img src="chevron.png"></div>');
} else {
jQuery('.post-content').find('img:first').wrap(jQuery("<div class='slideshow-wrapper'><a href=" + newrl + ">").attr("href", link));
}
});
任何想法,我缺少的是什麼? thx
是總是關於在URL的末尾位數(注意後面的數字)?或者是關於網址中的最後一位數字,並且可以跟隨數字? –
可能有查詢字符串參數。但路徑本身(沒有參數的url)將總是以我想增加的數字結尾 –