string locationPoints= "(29.67850809103362, 79.74288940429688),(29.367814384775375, 79.29519653320312),(29.561512529746768, 79.20455932617188),(29.69759650228319, 79.45449829101562)"; /* C# code */
success: function (data) { /* javascript code
var points = [];
points[0] = [25.1420, 55.1861];
points[1] = [25.001225, 55.657674];
points[2] = [25.3995, 55.4796]; .... }我将c#方法的上述定位点传递给jqueryajax成功函数。
I need to pass the locationPoints to javascriptMVCArray to draw polygon on map. here i have to store those locationPoints same as above points array. But i don't know that how to store. Please can anyone help me in that.?发布于 2015-12-10 06:41:48
所以你得用绳子弹奏一下。用逗号分隔,去掉额外的括号等。
var locationPoints= "(29.67850809103362, 79.74288940429688),(29.367814384775375, 79.29519653320312),(29.561512529746768, 79.20455932617188),(29.69759650228319, 79.45449829101562)";
var getPoints = function(){
var pairs = locationPoints.split('),');
var orderedPairs = [];
pairs[pairs.length-1] = pairs[pairs.length-1].slice(0,-1);
pairs.forEach(function(pair, index, array){
array[index] = pair.substring(1);
var orderedPair = array[index].split(',');
orderedPairs.push([parseFloat(orderedPair[0]), parseFloat(orderedPair[1])]);
});
orderedPairs.forEach(function(pair, index, arr){
$('#points').append('[' + pair[0] + ', ' + pair[1] + ']<br>');
});
}这里有一个与它有关的http://plnkr.co/edit/eYEwm84f5By40oYNpHNo?p=preview
发布于 2015-12-10 06:16:53
只是不要在你不需要的地方使用Regex。这里不是你需要Regex的地方,所以不要去那里。总结和澄清:不要为此使用正则表达式。
一些分割和修剪将完成同样的工作,速度要快得多:
// First we split by "),(" to get each of the points
var res = locationPoints.Splits(new[]{"),("})
// Each point-string will be handled to produce Lat and Lng
.Select(pt =>
{
// Remove '(' and ')' to clean first/last points
// And split by comma to get the two components
var s = pt.Trim('()').Split(',');
var lat = s[0].Trim();
var lng = s[1].Trim();
// Choose how to represent each point, example:
return new LatLng {Lat=lat, Lng=lng};
})
// Make an array out of that
.ToArray();发布于 2015-12-10 07:07:52
使用regex匹配,我们用简单的代码得到解决方案,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace testpro1
{
class Program
{
public string[] poarr = null;
static void Main(string[] args)
{
string strRegex = @"([0-9]*\.[0-9]+|[0-9]+)";
string strTargetString = @"(29.67850809103362, 79.74288940429688),(29.367814384775375, 79.29519653320312),(29.561512529746768, 79.20455932617188),(29.69759650228319, 79.45449829101562)";
//matches is the array
Match[] matches = Regex.Matches(strTargetString, strRegex)
.Cast<Match>()
.ToArray();
Console.WriteLine("The Array Values are:\n");
foreach (var item in matches)
{
Console.WriteLine(item.ToString());
}
Console.ReadLine();
}
}
}https://stackoverflow.com/questions/34194783
复制相似问题