当有两个时,正则表达式匹配来自URL的数据只返回1个匹配

时间:2017-03-07 07:15:36

标签: javascript arrays regex

根据我提交的网址,有两个匹配项。他们是 'en-gb'和'uk-ireland'

网址:https://www.myabcalphabeta.com/en-gb/destinations/uk-ireland

但是当我返回该函数时,它只返回最后一个,即'uk-ireland'

如何捕捉第一场比赛“en-gb”。我也看到循环工作正常,因为它返回两个值。

控制台输出的屏幕截图。 Screenshot of output in console.

    $(document).ready(function(){
        function getURLLocale() {
        var url = $(location).attr("href");
        var localeRegex = /^[a-z][a-z]-[a-z][a-z]/g;
        var urlBreakdown = url.split("/");
        console.log(urlBreakdown);
        var lang = "en";
        var country = "gb";

        for(var i = 0; i < urlBreakdown.length; i++) {
            if(urlBreakdown[i].match(localeRegex)) {
                lang = urlBreakdown[i].split("-")[0];
                country = urlBreakdown[i].split("-")[1];
                console.log(lang+ "\n");
            };
        }

        return([lang, country]);
    } // End function getURLLocale()
    console.log(getURLLocale());
})

3 个答案:

答案 0 :(得分:1)

我认为你可以这样做:

const url = `https://www.myabcalphabeta.com/en-gb/destinations/uk-ireland`

const getUrlData = url => {
  const en = url.match(/\ben-?\w*\b/)
  const uk = url.match(/\buk-?\w*\b/)
  const result = []
  if (en) result.push(en[0])
  if (uk) result.push(uk[0])
  return result
}

console.log(getUrlData(url))

或者这个

const url = `https://www.myabcalphabeta.com/en-gb/destinations/uk-ireland`

const getUrlData = url => {
  const regex = /\b[a-z]{2}-[a-z]*\b/g
  const result = []
  let temp

  while (temp = regex.exec(url)) {
    result.push(temp[0])
  }
  return result
}
console.log(getUrlData(url))

这会选择以2 [a-z]开头的任何部分,然后必须开始单词 - 以及所有[a-z],直到不是单词

答案 1 :(得分:1)

使用正则表达式exec从网址(\w{2}-\w{2,})获取所有区域设置。

$(document).ready(function () {
    function getURLLocale() {
        var url = 'https://www.myabcalphabeta.com/en-gb/destinations/uk-ireland';
        var m;
        var regex = /(\w{2}-\w{2,}(-\w{2})?)/g;
        var locales = [];

        while (m = regex.exec(url)) {
            locales.push(m[0]);
        }

        return locales;
    } // End function getURLLocale()
    console.log(getURLLocale());
});

该数组将包含两个语言环境。如果您只想获取第一个而不是getURLLocale(),请打印getURLLocale()[0]

修改

我将正则表达式更改为(\w{2}-\w{2,}(-\w{2})?),因此您可以匹配this列表中的任何可能的区域设置

答案 2 :(得分:0)

你的表情错了:

将正则表达式更改为^[a-z][a-z]-[a-z][a-z]$

Have a look

$(document).ready(function(){
        function getURLLocale() {
        var url = "https://www.myabcalphabeta.com/en-gb/destinations/uk-ireland";
        var localeRegex = /^[a-z][a-z]-[a-z][a-z]$/g;
        var urlBreakdown = url.split("/");
        console.log(urlBreakdown);
        var lang = "en";
        var country = "gb";

        for(var i = 0; i < urlBreakdown.length; i++) {
            if(urlBreakdown[i].match(localeRegex)) {
                lang = urlBreakdown[i].split("-")[0];
                country = urlBreakdown[i].split("-")[1];
                console.log(lang+ "\n");
            };
        }

        return([lang, country]);
    } // End function getURLLocale()
    console.log(getURLLocale());
})