Separate value that is inside a link

Viewed 75

The value that all the links I place in the text bar is from this model:

Text bar:

<input type="text" name="url" id="url" style="width: 282px;" />

Link model:

https://sports.hummerball.com/betting/en-gb/football/OB_EV21090572/decic-vs-drita

The value I want to recover is the one between OB_EV and /, in this example the value would be:

21090572

This retrieved value, I want it to be used in url.value so when I click the button, the correct link is loaded in the iframe.

How would the script look to be able to retrieve these values?
I have no knowledge of strings to separate these values.

My current script:

            <form action="" method="post" id="url-setter">
               <button type="submit" name="submit">Radar 1</button>
               <input type="text" name="url" id="url" style="width: 282px;" />
            <iframe id="the-frame" width="347" height="282" src=""></iframe>
            <script type="text/javascript">
               (function () {
                "use strict";
                var url_setter = document.getElementById('url-setter'), url = document.getElementById('url'), the_iframe = document.getElementById('the-frame');
                url_setter.onsubmit = function (event) {
                    event.preventDefault();
                    the_iframe.src = "https://sports.staticcache.org/scoreboards/scoreboards-football/index.html?eventId=" + url.value;
                };
               }());
            </script>
7 Answers

You can use a RegExp, specifically this pattern:

^^https:\/\/sports\.hummerball\.com\/betting\/[\w-]+\/football\/OB_EV(\d+)\/

Try it out here:

Usage:

const regexpObEv = /^https:\/\/sports\.hummerball\.com\/betting\/[\w-]+\/football\/OB_EV(\d+)\//;
const url = "https://sports.hummerball.com/betting/en-gb/football/OB_EV21090572/decic-vs-drita";
const match = url.match(regexpObEv);

console.log(match[1]);

Output:

21090572

You can use String#slice and String#indexOf.

let s = "https://sports.hummerball.com/betting/en-gb/football/OB_EV21090572/decic-vs-drita";
let i = s.indexOf('OB_EV');
let res = s.slice(i + 5, s.indexOf('/', i));
console.log(res);

var url = 'https://sports.hummerball.com/betting/en-gb/football/OB_EV21090572/decic-vs-drita';
console.log(url.match(/OB_EV\d+/g)[0].replace('OB_EV', ''));

Solution 1

Matching and parsing digit values

  let string = "https://sports.hummerball.com/betting/en-gb/football/OB_EV21090572/decic-vs-drita"

  function parseNum(str) {
    str = str.match(/(\d+)/)[0]
    str = parseInt(str);
    return str;
  }
 
  console.log(parseNum(string))

Solution 2

Removing all digits by simply replacing non-digits with empty string ' '

  let string = "https://sports.hummerball.com/betting/en-gb/football/OB_EV21090572/decic-vs-drita"
    
  function parseNum(str) {
    str = str.replace(/^\D+/g, '');
    str = parseInt(str);
    return str
  }

 console.log(parseNum(string))
 

You could get the pathname using URL and capture 1 or more digits using (\d+) from OB_EV21090572/ in capture group 1.

const url = new URL("https://sports.hummerball.com/betting/en-gb/football/OB_EV21090572/decic-vs-drita");
const m = url.pathname.match(/OB_EV(\d+)\//);
console.log(m[1]);

Regex is probably the way to go, but here is another explicit option.

const s = 'https://sports.hummerball.com/betting/en-gb/football/OB_EV21090572/decic-vs-drita';

const x = s.split('/');

// Seems like your target would always be in the 6th of the array that #split returns.

const y = x[6];

// Since 'OB_EV' is 5 chars, we can slice at that position.

const val = y.slice(5, y.length);

console.log(val);

Related