0

I'm trying to get subsection of a string(url) that looks like this.
https://justkidding.com/ids/09239849/dont-say-a-word and i want to get 09239849

*The length and value of the 09239849 (substring) varies but the string (url) structure remains the same.

I tried using replace method but i can only get 09239849/dont-say-a-word from the string because everything after the substring (09239849) is dynamic

<!--mycode-->
<button onclick="myFunction()">View ID</button>
<script>
function myTrim(x) {
return x.replace('https://justkidding.com/ids/','');
}

function myFunction() {
var str = myTrim("https://justkidding.com/ids/09239849/dont-say-a-word");
alert(str);
}
</script>
Giddy Naya
  • 2,202
  • 10
  • 23
  • https://stackoverflow.com/questions/45641931/increment-a-number-in-a-string-based-on-the-substring-before-it-in-js/45642092# – adeneo Aug 11 '17 at 19:20
  • You can do something like : const url = "https://justkidding.com/ids/09239849/dont-say-a-word"; const id = url.match(/\/\d+\//g)[0].replace(/\//g, ''); This regex will help you to extract this kind of pattern. Hope it will help ! – Yago Aug 11 '17 at 19:28
  • You can use string match and a regex : `function getNumberFromUrl(x) {return x.match(/\/(\d+)\//)[1]}`. More info on regex [here](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – LukStorms Aug 11 '17 at 19:32
  • Tanx @yago your snippet did the trick. I wonder why my question was marked as duplicate cus the link referred was not in anyway related. – Giddy Naya Aug 11 '17 at 19:40

0 Answers0