Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Converting an URL to JSON encoded format

Converting an URL to JSON encoded format

Scheduled Pinned Locked Moved JavaScript
javascriptcomjsonhelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jasonalien
    wrote on last edited by
    #1

    Hello there, I want to have a function that turns this : http://examplesite.com/folders/images/myphoto.jpg to this : http:\/\/examplesite.com\/folders\/images\/myphoto.jpg Can any of you help me with that? I'm not that good at JS but I need this : / Thanks.

    D M 2 Replies Last reply
    0
    • J jasonalien

      Hello there, I want to have a function that turns this : http://examplesite.com/folders/images/myphoto.jpg to this : http:\/\/examplesite.com\/folders\/images\/myphoto.jpg Can any of you help me with that? I'm not that good at JS but I need this : / Thanks.

      D Offline
      D Offline
      Dennis E White
      wrote on last edited by
      #2

      If all that you want is to replace a '/' with '\/' then I would recommend you look into the String.replace method you get with javascript.

      function repl(url) {
      return url.replace('/', '\/');
      }

      But since you mentioned you were trying to turn a URL into JSON I am thinking there is more to what you are asking. In which case I would recommend the following: http://james.padolsey.com/javascript/parsing-urls-with-the-dom/[^]

      J 1 Reply Last reply
      0
      • D Dennis E White

        If all that you want is to replace a '/' with '\/' then I would recommend you look into the String.replace method you get with javascript.

        function repl(url) {
        return url.replace('/', '\/');
        }

        But since you mentioned you were trying to turn a URL into JSON I am thinking there is more to what you are asking. In which case I would recommend the following: http://james.padolsey.com/javascript/parsing-urls-with-the-dom/[^]

        J Offline
        J Offline
        jasonalien
        wrote on last edited by
        #3

        Thank you

        U 1 Reply Last reply
        0
        • J jasonalien

          Thank you

          U Offline
          U Offline
          User 11056480
          wrote on last edited by
          #4

          Lovely !! thanks !! CLT20 LIVE STREAMING 2014

          Aloknath Jaiswal

          1 Reply Last reply
          0
          • J jasonalien

            Hello there, I want to have a function that turns this : http://examplesite.com/folders/images/myphoto.jpg to this : http:\/\/examplesite.com\/folders\/images\/myphoto.jpg Can any of you help me with that? I'm not that good at JS but I need this : / Thanks.

            M Offline
            M Offline
            MIG777
            wrote on last edited by
            #5
                //the string to work with:
                var httpAddress = 'http://examplesite.com/folders/images/myphoto.jpg',
                    //the char(s) to look for:
                    charToRemp = '/',
                    //the char(s) to change to:
                    rempCharWith = '\\\\/';
            
                //using RegExp
                function regexp\_changeCharsTo(charIn, from, to) {
                    var regExpFrom = RegExp(from, 'g');
                    return charIn.replace(regExpFrom, to);
                }
            
                //using array map
                function arrMap\_changeCharsTo(charIn, from, to) {
                    return charIn
                        .split('')
                        .map(function (chr) {
                            if (chr === from) {
                                return to;
                            }
                            return chr;
                        })
                        .join('');
                }
            
                //using for in loop
                function forIn\_changeCharsTo(charIn, from, to) {
                    var fnString = '';
                    for (var cr in charIn) {
                        if (charIn\[cr\] === from) {
                            fnString += to;
                        } else {
                            fnString += charIn\[cr\];
                        }
                    }
                    return fnString;
                }
            
                //Test:
                var regexp\_test = regexp\_changeCharsTo(httpAddress, charToRemp, rempCharWith);
                var arrMap\_test = arrMap\_changeCharsTo(httpAddress, charToRemp, rempCharWith);
                var forIn\_test = forIn\_changeCharsTo(httpAddress, charToRemp, rempCharWith);
            
            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups