Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Insert one string into another.

  1. #1
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default Insert one string into another.

    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:

    Code:
    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!

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Well, if you're looking for that file in specific:

    Code:
    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";
    - Mike

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    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:
    Code:
    Lang.interpolate("http://www.example.com/%LANG%/hello.html", {
        'LANG' : 'es'
      });
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I'm seriously starting to believe you have some kind of condition. Haha.
    - Mike

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Hmm? Because I respond to posts with (simple, in this case) code?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    You might find 5 people on this entire forum who think that code is simple. Me = 15 year old. You = Intelligent person.
    - Mike

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's four lines of actual code, and a few lines of namespacing Also, I'm only three years older than you.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Wow, really? Cool. You seem older.
    - Mike

  9. #9
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    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.

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    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...t_Initializers
    http://developer.mozilla.org/en/docs...ction_Operator
    http://developer.mozilla.org/en/docs...String:replace
    http://developer.mozilla.org/en/docs...ments:for...in
    http://developer.mozilla.org/en/docs...hasOwnProperty
    http://developer.mozilla.org/en/docs...nctions:RegExp
    Last edited by Twey; 08-08-2008 at 09:55 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •