PDA

View Full Version : Insert one string into another.



jlizarraga
08-08-2008, 07:32 PM
Hello everyone. Today I'm looking for a way to insert one string within a certain point in another, to make redirects between spanish and english pages dynamic.

Let's pretend I have the following two variables:


var string = "http://www.example.com/hello.html"; // This would come from document.location

var insert = "/newdir/"

How would I go about taking these two strings and creating the string "http://www.example.com/newdir/hello.html"?

Thanks a bunch!

mburt
08-08-2008, 07:36 PM
Well, if you're looking for that file in specific:


var string = "http://www.example.com/hello.html"; // This would come from document.location
var insert = "newdir"
string = string.replace("/hello.html", new RegExp(insert))+"hello.html";

Twey
08-08-2008, 08:01 PM
var Lang = {
interpolate: function(str, vars) {
str = str.replace(/%%/g, '\0');
for (var x in vars)
str = str.replace(new RegExp('%' + x + '%', 'g'), vars[x]);
return str.replace(/\0/g, '%');
}
};Usage:
Lang.interpolate("http://www.example.com/%LANG%/hello.html", {
'LANG' : 'es'
});

mburt
08-08-2008, 08:03 PM
I'm seriously starting to believe you have some kind of condition. Haha.

Twey
08-08-2008, 08:26 PM
Hmm? Because I respond to posts with (simple, in this case) code?

mburt
08-08-2008, 08:30 PM
You might find 5 people on this entire forum who think that code is simple. Me = 15 year old. You = Intelligent person.

Twey
08-08-2008, 08:38 PM
It's four lines of actual code, and a few lines of namespacing :rolleyes: Also, I'm only three years older than you.

mburt
08-08-2008, 08:46 PM
Wow, really? Cool. You seem older.

jlizarraga
08-08-2008, 09:10 PM
Is there any chance you could explain what is going on here line by line? There's a lot of stuff in there I'm completely unfamiliar with.

Twey
08-08-2008, 09:17 PM
var Lang = { // create a variable named 'Lang' and to it assign a new object.
interpolate: function(str, vars) { // create a function of two arguments and assign it to a property 'interpolate' within that object.
str = str.replace(/\\%/g, '\0'); // replace all instances of '\%' with a \0 null placeholder character
for (var x in vars) // for every key in 'vars' as 'x'
if (vars.hasOwnProperty(x))
str = str.replace(new RegExp('%' + x + '%', 'g'), vars[x]); // replace %x% with the value of that var in the 'vars' object
return str.replace(/\0/g, '%'); // swap the placeholder out for a single % and return
} // end function
}; // end object and statement

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Creating_New_Objects:Using_Object_Initializers
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Operators:Special_Operators:function_Operator
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:replace
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:for...in
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Object:hasOwnProperty
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Functions:RegExp

DimX
08-08-2008, 09:46 PM
Just one minor problem Twey :p, your code will fail in cases where several place-holders appear consecutively (eg. "%LANG%%SOMETHING%").

Twey
08-08-2008, 09:55 PM
True that. *edit*

mburt
08-09-2008, 02:59 AM
Is there any chance you could explain what is going on here line by line? There's a lot of stuff in there I'm completely unfamiliar with.

I think the OP is looking for an example script.

Twey
08-09-2008, 03:03 AM
I gave one...

mburt
08-09-2008, 03:03 AM
Usage of the script.

EDIT: Sorry! Missed the original post, my bad. Well I feel stupid now.

jlizarraga
08-11-2008, 06:01 PM
Okay, so this is the usage, right? :


Lang.interpolate("http://www.example.com/%LANG%/hello.html", {
'LANG' : 'es'
});

If the first argument, the URL, is coming from the URL of the current page, how do get from that to "http://www.example.com/%LANG%/hello.html"?

DimX
08-11-2008, 06:37 PM
Seems that this will be sufficient for you:


function reRoot(url, root)
{
return url.replace(/^([a-z]+:\/\/[^\/]+)(.*)/i, "$1" + root + "$2");
}
var newUrl = reRoot(document.location.href, "/newdir");

jlizarraga
08-13-2008, 04:05 AM
That's perfect, thank you!

jlizarraga
08-13-2008, 06:14 AM
I made this with your help:

http://ballhonda.autofusion.com/includes/gs.js

:)

Thanks again!

Trinithis
08-13-2008, 07:12 AM
You might find 5 people on this entire forum who think that code is simple.
It is simple ;)

Example usage:

var dirs = ["", "foo/", "bar/", "foo/baz/"];
for(var i = 0; i < dirs.length; ++i)
alert(Lang.interpolate("http://www.example.com/%DIR%hello.html", {
"DIR": dirs[i]
}));