zugo123456789
zugo123456789

Reputation: 107

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

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

Answers (1)

Taki
Taki

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

Related Questions