www.webdeveloper.com
Results 1 to 6 of 6

Thread: Parse postal address

  1. #1
    Join Date
    Dec 2012
    Posts
    4

    Parse postal address

    Let's say I have this string:

    HTML Code:
        <div>john doe is nice guy btw 8240 E marblehead way 92808 is also</div>
    or this string:
    HTML Code:
        <div>sky being blue? in the world is true? 123 Main St., apt c420 Anywhere, USA 12345 jackfroast nipping on the firehead</div>
    How would I go about parsing a string similar to the one of the strings above, extracting the address and document.write-ing the address? Would this involve some sort of regex code?

    I've tried looking online for a solution using jQuery/Javascript, but to no avail.
    And no other post here (as far as I know) provides a solution that uses jQuery and/or Javascript.

    Can somebody point me in the right direction? How would I go about accomplishing this in jQuery/JavaScript?

    Thank you in advance

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    Regular expressions would probably be the way to go. The actual code could however get very complex depending on how many different styles of addresses you'd want to detect.

    This link might help you.
    Last edited by ReFreezed; 12-27-2012 at 09:00 AM.
    New to web development or in need of a good reference? Check out the Mozilla Developer Network or W3Schools.

  3. #3
    Join Date
    Nov 2010
    Posts
    1,085
    you could try grabbing everything between the first and last occurrences of a number, but that will run into trouble if there is a number in the string that isn't part of the address...

    Code:
    <body>
     <div class="address">john doe is nice guy btw 8240 E marblehead way 92808 is also</div>
    <div class="address">sky being blue? in the world is true? 123 Main St., apt c420 Anywhere, USA 12345 jackfroast nipping on the firehead</div>
    <div class="address">I know 1 thing -  456 Second St., Somewhere, USA 16789 was the best place I ever lived</div>
    <div id="res"></div>
    <script>
    var divs=document.getElementsByTagName("div");
      for (var i = 0; i < divs.length; i++) {
      if(divs[i].className=="address"){
      var str=divs[i].innerHTML;
      var parsed=str.match(/\d(.*\d)/)[0];
      document.getElementById("res").innerHTML+=parsed+"<br>";
      }
    }
    </script>
    </body>

  4. #4
    Join Date
    Dec 2012
    Posts
    4
    Thanks, but the string would also be handling a lot of phone numbers...



    Quote Originally Posted by xelawho View Post
    you could try grabbing everything between the first and last occurrences of a number, but that will run into trouble if there is a number in the string that isn't part of the address...

    Code:
    <body>
     <div class="address">john doe is nice guy btw 8240 E marblehead way 92808 is also</div>
    <div class="address">sky being blue? in the world is true? 123 Main St., apt c420 Anywhere, USA 12345 jackfroast nipping on the firehead</div>
    <div class="address">I know 1 thing -  456 Second St., Somewhere, USA 16789 was the best place I ever lived</div>
    <div id="res"></div>
    <script>
    var divs=document.getElementsByTagName("div");
      for (var i = 0; i < divs.length; i++) {
      if(divs[i].className=="address"){
      var str=divs[i].innerHTML;
      var parsed=str.match(/\d(.*\d)/)[0];
      document.getElementById("res").innerHTML+=parsed+"<br>";
      }
    }
    </script>
    </body>

  5. #5
    Join Date
    Dec 2012
    Posts
    4
    Thanks, but this woudn't work as I would be handling a lot of phone numbers and dates.

  6. #6
    Join Date
    Dec 2012
    Posts
    4
    Ah, but this solution woudn't work, cause I'm also dealing with phone numbers and dates...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles