There is a two match according to the URL I submit. They are 'en-gb' and 'uk-ireland'
根据我提交的URL,有两个匹配项。他们是'en-gb'和'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'
但是当我返回该函数时,它只返回最后一个,即'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.
我如何抓住第一场比赛“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 个解决方案
#1
0
Your expression was wrong:
你的表达错了:
Changed regex to ^[a-z][a-z]-[a-z][a-z]$
将正则表达式更改为^ [a-z] [a-z] - [a-z] [a-z] $
看一看
$(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());
})
#2
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
这将选择以2 [a-z]开头的任何部分,然后必须开始单词 - 以及所有[a-z]直到不是单词
#3
1
To get all the locales from the url exec
with the regex (\w{2}-\w{2,})
.
使用正则表达式(\ w {2} - \ w {2,})从url exec获取所有语言环境。
$(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]
.
该数组将包含两个语言环境。如果你只想得到第一个,而不是getURLLocale(),请打印getURLLocale()[0]。
EDIT
I changed the regex to (\w{2}-\w{2,}(-\w{2})?)
, so you can match any possible locale from this list
我将正则表达式更改为(\ w {2} - \ w {2,}( - \ w {2})?),因此您可以匹配此列表中的任何可能的区域设置
#1
0
Your expression was wrong:
你的表达错了:
Changed regex to ^[a-z][a-z]-[a-z][a-z]$
将正则表达式更改为^ [a-z] [a-z] - [a-z] [a-z] $
看一看
$(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());
})
#2
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
这将选择以2 [a-z]开头的任何部分,然后必须开始单词 - 以及所有[a-z]直到不是单词
#3
1
To get all the locales from the url exec
with the regex (\w{2}-\w{2,})
.
使用正则表达式(\ w {2} - \ w {2,})从url exec获取所有语言环境。
$(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]
.
该数组将包含两个语言环境。如果你只想得到第一个,而不是getURLLocale(),请打印getURLLocale()[0]。
EDIT
I changed the regex to (\w{2}-\w{2,}(-\w{2})?)
, so you can match any possible locale from this list
我将正则表达式更改为(\ w {2} - \ w {2,}( - \ w {2})?),因此您可以匹配此列表中的任何可能的区域设置