<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?
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.
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>
Thanks, but the string would also be handling a lot of phone numbers...
Originally Posted by xelawho
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>
Bookmarks