我想用javascript来提取网址的数字部分.只有一套数字(也就是说 - 它永远不会是www.example.com/455/all/6).这组数字并不总是相同的.我怎样才能做到这一点?我知道我可以使用这段代码
var urlPath = window.location.pathname;
Run Code Online (Sandbox Code Playgroud)
提取网址,但我不知道如何进一步.
urlPath.match(/\d+/)[0]会得到数字.但是,它不执行任何健全性检查,如果根本没有数字,则会失败.要获得更好的结果,请尝试
var t = urlPath.match(/\d+/);
t = t ? t[0] : null; // or some other default value, such as `0`
Run Code Online (Sandbox Code Playgroud)