Reputation: 107
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
And this is the substring that I want to extract:
abcdefg?hijk?lmnop
_
I tried using this code:
currenturl.substring(currenturl.lastIndexOf("?") + 1, currenturl.lastIndexOf("?"));
But it only returns "?"
Thanks for any advice!
Upvotes: 1
Views: 51
Reputation: 17664
You should use indexOf
which returns the index of the first matching ?
as the first param to subString
:
const currenturl = "abcdefg?hijk?lmnop";
const result = currenturl.substring(currenturl.indexOf("?") + 1, currenturl.lastIndexOf("?"));
console.log(result);
Upvotes: 4