你可以使用正则表达式来提取最后的2。以下是一个使用 JavaScript 的例子:

const str = "DBI2-2";
const regex = /(\d+)$/; // 匹配最后的数字
const matches = str.match(regex);

if (matches) {
  const lastNumber = matches[1]; // 匹配到的数字
  console.log(lastNumber);
} else {
  console.log("未找到匹配的数字");
}

输出结果为:2

js 从字符串DBI2-2中提取最后的2 正则

原文地址: https://www.cveoy.top/t/topic/h0Zh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录