【问题标题】:Javascript get subsring between two of the same characters?Javascript获取两个相同字符之间的子字符串?
【发布时间】:2019-04-15 14:54:22
【问题描述】:

我正在尝试从存在于两个相同字符之间的另一个字符串中提取子字符串。

这是我的整个字符串:

abcdefg?hijk?lmnop

这是我要提取的子字符串:

abcdefg?hijk?lmnop

_

我尝试使用此代码:

currenturl.substring(currenturl.lastIndexOf("?") + 1, currenturl.lastIndexOf("?"));

但它只返回“?”

感谢您的建议!

【问题讨论】:

    标签: javascript string substring


    【解决方案1】:

    您应该使用indexOf,它将第一个匹配的? 的索引作为subString 的第一个参数返回:

    const currenturl = "abcdefg?hijk?lmnop";
    const result = currenturl.substring(currenturl.indexOf("?") + 1, currenturl.lastIndexOf("?"));
    
    console.log(result);

    【讨论】:

    • 谢谢!效果很好
    猜你喜欢
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 2014-04-02
    • 2018-09-22
    • 2013-01-29
    • 1970-01-01
    • 2013-09-12
    相关资源
    最近更新 更多