JSFiddle - React, Tailwind, and code Playground
by jfriend00
CSS
body {font-family: "Courier New"; font-size: 13px;}
JavaScript
var tests = [
"http://example.com/whatever#page?x=1&locale=hu&y=2",
"http://example.com/whatever#page?x=1&locale=hu",
"http://example.com/whatever#page?locale=hu",
"http://example.com/whatever#page?locale=",
"http://example.com/whatever#page?x=1",
"http://example.com/whatever#page",
"http://example.com/whatever",
// other test cases I added
"http://example.com/whatever#page?xlocale=foo&x=1&locale=hu&y=2",
"http://example.com/whatever?locale=nomatch#page?x=1&locale=hu&y=2",
"http://example.com/locale/whatever?locale=nomatch#page?x=1&locale=hu&y=2"
];
function getLocaleFromHash(url) {
var match = url.match(/#.*[?&]locale=([^&]+)(&|$)/);
return(match ? match[1] : "");
}
var results = [];
for (var i = 0; i < tests.length; i++) {
results.push('"' + getLocaleFromHash(tests[i]) + '"');
}
document.body.innerHTML = results.join("<br>");