我正在尝试从存在于两个相同字符之间的另一个字符串中提取一个子字符串。
这是我的整个字符串:
abcdefg?hijk?lmnop
这是我要提取的子字符串:
abcdefg?hijk?lmnop
_
我试过使用这段代码:
currenturl.substring(currenturl.lastIndexOf("?") + 1, currenturl.lastIndexOf("?"));
但它只返回“?”
感谢您的任何建议!
最佳答案
您应该使用 indexOf
返回第一个匹配的 ?
的索引作为 subString
的第一个参数:
const currenturl = "abcdefg?hijk?lmnop";
const result = currenturl.substring(currenturl.indexOf("?") + 1, currenturl.lastIndexOf("?"));
console.log(result);
关于Javascript 在两个相同字符之间获取子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55691881/