0

I have a giant source file in HTML with javascript I'd like to extract some information from a javascript content. Considering I have a file like the following

<h1>Hello World</h1>
<script type="text/javascript">something useless</script>
<script type="text/javascript">var gmaps_vars = {FullAddress: "Rio de Janeiro, Rio de Janeiro, Brasil", GeoLocation: {"lng":-43.187554,"lat":-22.9470974}}</script>

How would I extract the latitude and longitude?

2
  • What do you mean fetch the var? Commented Jul 26, 2014 at 19:51
  • I've updated with the example I really need. I tried to be too generic at first. :) Commented Jul 26, 2014 at 19:55

2 Answers 2

1

How about using regular expression:

m = /GeoLocation: \{"lng":(-?\d+\.\d+),"lat":(-?\d+\.\d+)\}/.match(html)
lng = m[1]
lat = m[2]
Sign up to request clarification or add additional context in comments.

Comments

0

Try

html

<h1>Hello World</h1>
<script type="text/javascript">var a;</script>
<script type="text/javascript" id="vars">var gmaps_vars = {"FullAddress": "Rio de Janeiro, Rio de Janeiro, Brasil", "GeoLocation": {"lng":-43.187554,"lat":-22.9470974}}</script>

js

$(function() {
    var coords = [];
    $.post(url, function(data, textStatus, jqxhr) {
        $.each($(data), function(index, value) {
            if ($(value).is("#vars")) {
              coords.push(JSON.parse($(value).text().replace(/var|gmaps_vars|\=/g, "")).GeoLocation);
              console.log(coords[0].lng + "\n" + coords[0].lat);
            };
        });
    });
});

jsfiddle http://jsfiddle.net/guest271314/HzPFg/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.