javascript - Javascript在两个相同字符之间获得subsring?
问题描述
我正在尝试从两个相同字符之间存在的另一个字符串中提取一个子字符串。
这是我的整个字符串:
abcdefg?hijk?lmnop
这是我要提取的子字符串:
abcdefg? hijk lmnop
_
我尝试使用此代码:
currenturl.substring(currenturl.lastIndexOf("?") + 1, currenturl.lastIndexOf("?"));
但它只返回“?”
感谢您的任何建议!
解决方案
您应该使用indexOf
which 返回第一个匹配的索引?
作为第一个参数subString
:
const currenturl = "abcdefg?hijk?lmnop";
const result = currenturl.substring(currenturl.indexOf("?") + 1, currenturl.lastIndexOf("?"));
console.log(result);
推荐阅读
- swift - SwiftUI 在预览 macOS 目标时排除 Swift 包
- c++ - 如何在 gmock 中测试失败
- r - 如何将for循环与我在R中制作的函数结合起来
- go - 如何将 tlsConfig 与 RoundTripper 一起使用?
- python - 非常基本的 Python 语法
- python - 如何在Python中获取列表中重复元素的最高索引值
- java - 如何处理在 finally 块中关闭连接时引发的异常?
- java - 使 JLabel 文本与中心垂直对齐
- html - 我尝试在使用 angularjs 上传多个图像之前进行预览,但没有一个显示?
- c++ - std::shared_mutex 通过 thread_local 存储递归保护