I want to parse the 2 first elements of a URL with JavaScript.
For ex.
url.de/de/de
/produkte/
I want to parse de/de
, I have following code
function(){
var path={{Page Path}};
return path.match(/^/(w+)(?:/|$)/)[1]
}
but the result is just de
, hope for a easy solution.
I’ve tried a inline JavaScript
2
Answers
Parenthesis surrounding expression is looking for: 2 characters of a-z then
/
then 2 characters again.If you are looking for an alternative way without using pregmatch you can try something like this to parse the first to parts of the URL. For this answer its necessary for the searched string to be on the same position every time.
If you want to use pregmatch try something like this
[a-z]{2}/[a-z]{2}/
and replace the last ‘/’ with .replace(‘/’,”); to get rid of it.The Numbers in curly brackets are for the length of the searched part so if its only country codes it should be fine with sticking with 2.