简体   繁体   English

Javascript是否在两个相同字符之间加减号?

[英]Javascript get subsring between two of the same characters?

I'm trying to extract a substring from another string that exists between two of the same characters. 我试图从存在于两个相同字符之间的另一个字符串中提取一个子字符串。

This is my entire string: 这是我的整个字符串:

abcdefg?hijk?lmnop abcdefg?hijk?lmnop

And this is the substring that I want to extract: 这是我要提取的子字符串:

abcdefg? abcdefg? hijk ?lmnop hijk ?lmnop

_ _

I tried using this code: 我尝试使用此代码:

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

But it only returns "?" 但是它只返回“?”

Thanks for any advice! 感谢您的任何建议!

You should use indexOf which returns the index of the first matching ? 您应该使用indexOf ,它返回第一个匹配项的索引? as the first param to subString : 作为subString的第一个参数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM