link 中的单独值

Separate value that is inside a link

我在文本栏中放置的所有 link 的值均来自此模型:

文本栏:

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

Link 型号:

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

我要恢复的值是 OB_EV/ 之间的值,在本例中该值将是:

21090572

这个检索到的值,我希望它在 url.value 中使用,所以当我单击按钮时,正确的 link 被加载到 iframe

脚本如何检索这些值?
我不知道用字符串分隔这些值。

我当前的脚本:

            <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>
  • 使用split()函数。

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

//this would return -> 21090572/decic-vs-drita
let value = link.split("OB_EV")[1];

//this would return the real value -> 21090572
value = value.split("/")[0];

console.log(value);

更多信息位于:https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/String/split

您可以使用 RegExp,特别是这个模式:

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

Try it out here:

用法:

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]);

输出:

21090572

您可以使用 String#sliceString#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', ''));

解决方案 1

匹配和解析数字值

  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))

解决方案 2

通过简单地将非数字替换为空字符串来删除所有数字' '

  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))
 

您可以使用 URL 获取路径名,并使用 OB_EV21090572/ 中的 (\d+) 在捕获 组 1 中捕获 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]);

正则表达式可能是可行的方法,但这是另一个明确的选项。

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);