js获取后第一个字母或数字的下标
可以使用正则表达式来实现这个功能。以下是一个示例代码:
function getIndexAfterColon(str) {
var pattern = /:(\w)/;
var match = str.match(pattern);
if (match) {
return str.indexOf(match[1], match.index + 1);
}
return -1;
}
var str = "abc:def:123";
console.log(getIndexAfterColon(str)); // 输出:7
这段代码中,getIndexAfterColon函数接受一个字符串作为参数。它使用正则表达式/:(\w)/来匹配冒号后面的第一个字母或数字。如果匹配成功,那么match变量会保存匹配的结果信息。我们可以利用match对象的index属性来获取冒号的下标,然后使用indexOf函数来查找冒号后面的第一个字母或数字的下标。如果匹配失败,那么函数会返回-1。
原文地址: http://www.cveoy.top/t/topic/iTaX 著作权归作者所有。请勿转载和采集!