gpt4 book ai didi

javascript - 正则表达式不转义特殊字符

转载 作者:行者123 更新时间:2023-11-30 20:00:00 25 4
gpt4 key购买 nike

如果我这样做

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

我明白了

45, 9)

虽然我应该得到

45, 9

为了能够像这样将其拆分为 2 个 input .val():

    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);
}

完整代码:

// 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;
}
}

最佳答案

您的正则表达式应为:/(-?\d+(\.\d+)?)/g 以便仅匹配后跟可选小数部分的数字。

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

console.log("L.LatLng {lat: 45, lng: 9.1}".match(regex));

关于javascript - 正则表达式不转义特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53485395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com