I want to extract all digits after last occurrence of character "-" so for example 311-1974-8 should return me 8 and 311-1974-348 should return 348
edit:
Added clarification from a comment:
actually it's an external tool which provides it's own inbuild functionalists and i have no other option but to use regex to extract this. No JS can be applied :(
This captures the last number.
var str = '311-1974-348';
var matches = str.match(/-(\d+)$/);
var match = matches ? matches[1] : null;
console.log("matched? " + match);
Try matching on /[^-]+$/
, eg:
var s = '311-1974-348';
s.match(/[^-]+$/); // => ["348"]
为什么不简单地分裂呢?
var str = input.split('-').pop();
You mentioned in a comment it's for an external tool so...
-([0-9]+)$
dunno how your tool handles captured groups or anything...
Try this /[0-9]+$/
. It worked on both the inputs you provided.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.