Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
所以我列出了像 favoriteImage1、 favoriteImage2... favoriteImage22 这样的对象。如何获得单词末尾的数字?我试过 parseInt 但它返回未定义。是否有捷径可寻?
使用正则表达式:
var string = "favoriteImage1"; var num = +string.replace(/\D/g, "");
如果名称始终带有前缀“favoriteImage”,您也可以这样做
var x = "favoriteImage1"; var num = parseInt(x.substring(13));