1

If I do

if(childMarkers.length > 0) {
  const [lat, lng] = `${ childMarkers[0].getLatLng() }`.match(/(-?\d+.\d*)/gi);   
  const requiredString = `${ lat } ${ lng }`;
  console.log(requiredString);
}  

I get

45, 9)

While I should get

45, 9

In order to be able to split it into 2 input .val() like this:

    if(childMarkers.length > 0) {
      const [lat, lng] = `${ childMarkers[0].getLatLng() }`.match(/(-?\d+.\d*)/gi);   
      const requiredString = `${ lat } ${ lng }`;
      $("#longiTude").attr("value",lat);
      $("#latiTude").attr("value", lng);  
    }

FULL CODE:

// We draw the markers
function drawMarkers() {
  var i;
  for (i = 0; i < longitude.length; ++i) {
    pair=[ parseFloat( latitude[i] ) , parseFloat(  longitude[i]  ) ]
    count.push(  pair );
    $("#searchNations").removeAttr("disabled");
    $(this).attr("disabled", "disabled");
    var myYears = $('#years').val();
    $("#ajax-load-more ul").attr("data-meta-value", myYears);
  };
  if(stopAjax == false) {
    console.log("ciao");
    L.MarkerCluster.include({
      spiderfy: function(e) {
        var childMarkers = this.getAllChildMarkers();
        this._group._unspiderfy();
        this._group._spiderfied = this;
        // If there are any childMarkers
        if(childMarkers.length > 0) {
          // Match the lat and lng numbers from the string returned by getLatLng()
          const [lat, lng] = `${ childMarkers[0].getLatLng() }`.match(/(-?\d+(\.\d+)?)/g);   
          // Construct the required string value from the extracted numbers
          const requiredString = `${ lat } ${ lng }`;
          // Use requiredString to populate the value attribute of the input field in OP
          $("#longiTude").attr("value",lat);
          $("#latiTude").attr("value", lng);  
          console.log(requiredString);
          //submitSearchForm();
        }
      },
      unspiderfy: function() {
        this._group._spiderfied = null;
      }
    });

    var mcg = L.markerClusterGroup().addTo(map);
    circles = new L.MarkerClusterGroup();

    for (var i = 0; i < count.length; i++) {
      var a = count[i];
      var circle = new L.CircleMarker([a[0], a[1]]);
      circles.addLayer(circle);
      circle.on('click', function (e) {
        var curPos = e.target.getLatLng();
        $("#longiTude").val(curPos.lat);
        $("#latiTude").val(curPos.lng);
        console.log(curPos.lng);
        //submitSearchForm();
      });
    }
    // we add the markers to the map
    map.addLayer(circles);
    // we empty the arrays for the future calls
    count = [];
    longitude = [];
    // we set again stopAjax var to true to reset
    stopAjax = true;   
  }
}
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • Can you please edit your question and remove unrelated code (like the `const` rows in the first block) and fix the output (`45, 9}` instead of `45, 9)`) – Andreas Nov 26 '18 at 16:39
  • Possible duplicate of [What special characters must be escaped in regular expressions?](https://stackoverflow.com/questions/399078/what-special-characters-must-be-escaped-in-regular-expressions) – Andreas Nov 26 '18 at 16:40
  • It might be helpful to mention what kind of object `childMarkers` contains. – Mark Nov 26 '18 at 16:41
  • @Andreas `45, 9)` is what the console gives me, why should I remove if that is the question I am asking: hwo to remove that ) ? – rob.m Nov 26 '18 at 16:41
  • @MarkMeyer I have pasted what `console.log(childMarkers[0].getLatLng());` gives me – rob.m Nov 26 '18 at 16:41
  • The output is `L.LatLng {lat: 45, lng: 9}` and with that there won't magically appear a `)` – Andreas Nov 26 '18 at 16:42
  • @Andreas indeed, unless you console.log something else as per the question code – rob.m Nov 26 '18 at 16:42
  • What I’m saying is that you haven’t given us enough to reproduce the problem. `childMarkers` seems to contain some objects with a `getLatLng()` method. What are those? At least add a tag to help. – Mark Nov 26 '18 at 16:45
  • @MarkMeyer quite simply, I might simply need to ask: how do I escape ) in my regular expression? I will update the question – rob.m Nov 26 '18 at 16:46
  • 1
    Is `const { lat, lng } = childMarkers[0].getLatLng()` an option? Or what is the returned type of `childMarkers[0].getLatLng()` – Matthi Nov 26 '18 at 16:48
  • @Matthi question updated – rob.m Nov 26 '18 at 16:50
  • @rob.m my hunch is this a leaflet [latlng](https://leafletjs.com/reference-1.3.4.html#latlng) object -- so right now this looks like an xy problem. Rather than convert to a string and the use regex, you should just be grabbing the `lat` and `log` properties. But we can't be sure unless you indicate that that is. – Mark Nov 26 '18 at 16:52
  • @MarkMeyer exactly, but I am using the cluster, and I can't get separated lat and log of a cluster, I can't find anything on the docs, so I am trying to use a regex, updated the question with the full code, is there anything I could console.log in order to see if we can get separated lat and log of the cluster? – rob.m Nov 26 '18 at 16:55

1 Answers1

1

Your regex should be: /(-?\d+(\.\d+)?)/g in order to match only the numbers followed by an optional decimal part.

const regex = /(-?\d+(\.\d+)?)/g;

console.log("L.LatLng {lat: 45, lng: 9.1}".match(regex));
Alex G
  • 1,897
  • 2
  • 10
  • 15
  • The part of this answer that answers the question is escaping the period in the regex: `\.`. This is because a period `.` in regular expressions matches any character. – Billy Brown Nov 26 '18 at 16:52
  • 1
    @BillyBrown You also need the optional grouping for the decimal part since it may not be there. Otherwise it will only match numbers with decimals. – Alex G Nov 26 '18 at 16:54
  • @AlexG Oh definitely, and I was putting that in my own answer before I saw yours, but I think that the main misunderstanding in the question was the meaning of `.` in regular expressions. – Billy Brown Nov 27 '18 at 08:54