0

I know this is a super easy question, but I can't seem to wrap my head about it. I've got a bunch of URLs in varying languages such as:

www.myurl.com?lang=spa

www.myurl.com?lang=deu

www.myurl.com?lang=por

I need to create buttons to quickly switch from any language extension (spa, por, deu, rus, ukr, etc) to another language. I have the following code so far:

var url = window.location.toString();
window.location = url.replace(/lang=xxx/, 'lang=deu');

I just can't figure out the 3-character wildcard character. I know that I need to do some sort of regular expression or something, I'm just not sure how to go about it. Any help?

Thanks in advance

1

3 Answers 3

1

You can use

([&?]lang=)\w+

This will work with urls like www.myurl.com?foo=bar&lang=por&bar=foo too.

Instead of lang=deu, you'll have to replace with $1deu.

Sign up to request clarification or add additional context in comments.

Comments

1

Try ... or .{3} or \w{3} or even [a-z]{3}, depending on how specific you want to be.

var s = 'www.myurl.com?lang=spa';
s.replace(/lang=[a-z]{3}/, 'lang=deu');
// => "www.myurl.com?lang=deu"

Here's a railroad diagram of the above example:

enter image description here

Comments

0

Use /lang=[a-z][3}/, here's an example:

/lang=[a-z]{3}/

Regular expression visualization

Debuggex Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.