簡體   English   中英

如何獲取最后一個/特殊字符后的數字

[英]How to get digits after the last / special character

我需要從包含鏈接的字符串中獲取最后一位數字(最后一個數字)。 例如,從下面的鏈接中,我應該返回數字51

http://radio-pendimi.com/website/index.php/artikuj/31-miresjellja-edukata-fetare/51-permbaje-gjuhen-tende

或從以下一個:

http://radio-pendimi.com/website/index.php/artikuj/30-ilace-per-zemren-rukja/9137-jeta-me-allahun-ka-shije-tjeter, 

編號9137應該返回

$.each (path.item,function(key,val){
    //id_temp= val.guid.content.replace(/[^0-9 ]/g, '');
    id=val.guid.content;

    var tempDiv = $('<div id="tempDiv">' + val.description + '</div>');
    introImg = tempDiv.find("img:first").css({"width":"100%","border-bottom":"4px solid #cd1d1d"});
    var staticImg = '<img src="images/pendimi-logo.png" style="width:100%;border-bottom:4px solid #cd1d1d;background:#444"/>';
    output+='<div id="lajme">ID = '+id;
    output+= '<p style="height:15px;"><span style="font-size:13px;font-family:Bitter;color:gray;padding-left:7px">Kategoria: ' + val.category + '</span><span style="float:right;font-size:13px;font-family:Bitter;color:gray;margin:3px"></p>';
    output+='<a href="#artikujtFetar-full" onclick="showPostArtikujtFetar('+ id +')" style="text-decoration:none">';
    output+= '<p style="margin:0px">' + $('<div>').append(introImg).html() + '</p>';
    output+='<h3 style="word-wrap:break-word;font-family:Bitter;font-size:16px;color:#444;text-align:center;line-height:30px;">'+ val.title +'</h3>';
    output+='</a>';
    output+='</div>';
    output+='<div style="height:15px;background:rgb(210, 215, 217);"></div>';
    return key<3;
}); //lexo cdo element 1 nga 1
$('#shfaqArtikujtFetar').html(output);

PS:我需要使用jQuery或JavaScript。 它可以是正則表達式或函數,只需讓它返回我需要的結果即可。

.*\/(\d+)

這將給出所需的答案。

參見演示。

http://regex101.com/r/cH8vN2/3

另一種解決方案

\d+(?!.*\d)

用法

var s = "http://radio-pendimi.com/website/index.php/artikuj/31-miresjellja-edukata-fetare/51-permbaje-gjuhen-tende";
var p = /\d+(?!.*\d)/;
var m = s.match(p);
console.log(m[0]); // 51

正則表達式演示 | JavaScript演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM