Jump to content
  • Advertisement
Sign in to follow this  
hkBattousai

[RESOLVED][JavaScript] Help needed on regular expressions

This topic is 3703 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

function changeLang()
{
	var obj_lang = document.getElementById('lang_sel');
	var selIndex = obj_lang.selectedIndex;
	var selValue = obj_lang.options[selIndex].value;
	
	var url = document.URL;
	var langQuery = "lang=" + selValue;
	
	alert(url);			// outputs: "http://localhost/stirve/?lang=jp"
	alert(selValue);		// outputs: "en"
	var new_url = url;
	new_url.replace(/(lang=[a-z]{[[[2]]]})/, new_url);
	alert(new_url);			// outputs: "http://localhost/stirve/?lang=jp"
	
	// the rest of the function is yet to implement
}


This is a function which will change a string in 'url' variable to 'new_url' variable. For example, url = "http://localhost/stirve/?lang=jp"; selValue = "en"; The replace() method of 'new_url' object is expected to change "lang=jp" part of the string to "lang=en", but it doesn't. The value in 'new_url' does not change after calling the replace() method. Am I doing a mistake in regular expression format? Can anybody tell me what I am missing please? [Edited by - Battousai on October 27, 2008 3:50:23 PM]

Share this post


Link to post
Share on other sites
Advertisement
Opps, my bad. I saw my mistake after posting the message. I didn't give the variable 'selValue' into the replace() method.

But still I don't know how to do this. This is my first time using regular expressions in my code. Can you guys show me a way to do this please?

Share this post


Link to post
Share on other sites
Try this:
var new_url = url.replace(/lang=[a-z]{2}/, 'lang=' + selValue);
string.replace returns a value, it does not change the value of string.

Share this post


Link to post
Share on other sites
Thank you, my code works now.
function changeLang()
{
// read values in the controls
var obj_lang = document.getElementById('lang_sel');
var selIndex = obj_lang.selectedIndex;
var selValue = obj_lang.options[selIndex].value;

// find the language query to replace the current one
var langQuery = "lang=" + selValue;

// find the URL to locate
var url = document.URL;
var new_url = url.replace(/(lang\=[a-z]{[2]})/, langQuery);

// change the current URL
document.location = new_url;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!