1

There is a two match according to the URL I submit. They are 'en-gb' and 'uk-ireland'

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

But when I return the function it is returning only the last one, which is 'uk-ireland'

How do I catch the first match "en-gb". I also see the the loop is working fine since it is returning both value.

Screenshot of output in console. 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());
})
Santosh
  • 3,477
  • 5
  • 37
  • 75

3 Answers3

1

I think you can do this:

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

Or this

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

This selects any part starting with 2 [a-z] thath must start the word then - and all [a-z] until not word

Maciej Kozieja
  • 1,812
  • 1
  • 13
  • 32
1

To get all the locales from the url exec with the regex (\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());
});

The array will contain the two locales. if you want to get only the first, instead of getURLLocale(), print getURLLocale()[0].

EDIT

I changed the regex to (\w{2}-\w{2,}(-\w{2})?), so you can match any possible locale from this list

Community
  • 1
  • 1
Vasileios Pallas
  • 4,801
  • 4
  • 33
  • 50
0

Your expression was wrong:

Changed regex to ^[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());
})
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • Your answer is correct. Can you also explain a little bit about the dollar sign ? Does it refer to the last search item ? is it searching from right to left direction ? – Santosh Mar 07 '17 at 08:16
  • How do I match on twoletter-twoletter any idea ? – Santosh Mar 07 '17 at 08:17
  • @Santosh `^[a-z][a-z]-[a-z][a-z]$` is for `twoletter-twoletter`. `^` is start of string and `$` is end of string. In the above expression which i have mentioned says string should match twoletter-twoletter from start to end. – Sahil Gulati Mar 07 '17 at 08:44
  • then why it is matching 'uk-ireland' since 'ireland' has 7 letters ? – Santosh Mar 07 '17 at 08:48
  • 1
    @Santosh it is not matching `uk-ireland`. You have split your URL with `/`. That is why it is coming in array while looping over it. – Sahil Gulati Mar 07 '17 at 08:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137415/discussion-between-sahil-gulati-and-santosh). – Sahil Gulati Mar 07 '17 at 08:51