删除所有其他逗号并用括号括起来

时间:2015-10-17 19:49:05

标签: javascript jquery coordinates gis

所以使用jquery,我有一串坐标,如下所示:

38.313072,-89.863845,38.312675,-89.863586,38.310405,-89.862091,38.310405,-89.862091,38.309913,-89.861976,38.309768,-89.861976,38.309768,-89.861976

我需要它们看起来像这样:

[38.313072, -89.863845] [38.312675, -89.863586] [38.310405, -89.862091] [38.310405,-89.862091] [38.309913, -89.861976] [38.309768, -89.861976] [38.309768, -89.861976] [38.30965, -89.861991]

所以我需要弄清楚如何用空格和括号坐标集替换每个其他逗号。

想法?

5 个答案:

答案 0 :(得分:3)

您可以使用正则表达式。

([-\d.]+),([-\d.]+),?

Regex Explanation and Live Demo

  1. [-\d.]:字符类-将匹配文字-连字符,\d将匹配单个数字,.将匹配.从字面上。在班级序列中提到并不重要。
  2. +:匹配之前匹配的一个或多个匹配项
  3. (...):捕获群组。大括号内的匹配项被捕获并返回$1$2,...
  4. ,?:与其他所有逗号不匹配
  5. g:全球比赛。匹配所有可能的事件。
  6. jsFiddle Demo

    
    
    var str = '38.313072,-89.863845,38.312675,-89.863586,38.310405,-89.862091,38.310405,-89.862091,38.309913,-89.861976,38.309768,-89.861976,38.309768,-89.861976';
    
    var result = str.replace(/([-\d.]+),([-\d.]+),?/g, '[$1, $2] ').trim();
    document.getElementById('output').innerHTML = JSON.stringify(result, 0, 2);
    
    <pre id="output"></pre>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:1)

使用正则表达式! 这是完美的!

var str = "38.313072,-89.863845,38.312675,-89.863586,38.310405,-89.862091,38.310405,-89.862091,38.309913,-89.861976,38.309768,-89.861976,38.309768,-89.861976,38.30965, -89.861991";

var expected = "[38.313072, -89.863845] [38.312675, -89.863586] [38.310405, -89.862091] [38.310405,-89.862091] [38.309913, -89.861976] [38.309768, -89.861976] [38.309768, -89.861976] [38.30965, -89.861991]"


var computed = str.replace(/(([0-9\-. ]*)(,)([0-9\-. ]*)),?/g, '[$2 $3 $4] ');

document.write('computed<br>')
document.write(str.replace(/(([0-9\-. ]*)(,)([0-9\-. ]*)),?/g, '[$2 $3 $4] '))



document.write( "<hr>expected<br>" + expected  )

答案 2 :(得分:0)

不漂亮,但确实有效:

// C++ program to generate binary numbers from 1 to n
#include <iostream>
#include <queue>
using namespace std;

// This function uses queue data structure to print binary numbers
void generatePrintBinary(int n)
{
// Create an empty queue of strings
queue<string> q;

// Enqueue the first binary number
q.push("1");

// This loops is like BFS of a tree with 1 as root
// 0 as left child and 1 as right child and so on
while (n--)
{
    // print the front of queue
    string s1 = q.front();
    q.pop();
    cout << s1 << "\n";

    string s2 = s1;  // Store s1 before changing it

    // Append "0" to s1 and enqueue it
    q.push(s1.append("0"));

    // Append "1" to s2 and enqueue it. Note that s2 contains
    // the previous front
    q.push(s2.append("1"));
}
}
// Driver program to test above function
    int main()
    {
    int n;
    printf("Por favor ingrese un numero\n");
    scanf(" %d" ,&n);
    generatePrintBinary(n);
    return 0;
    }

答案 3 :(得分:0)

这应该做的工作。

&#13;
&#13;
function toPairs(src){
  // Split the string into values.
  var values = src.split(',');
  
  // Group these values 2 by 2.
  var pairs = [];
  for(var i = 0; i < values.length; i += 2){
    pairs.push("[" + values[i] + ", " + values[i + 1] + "]");
  }
  
  // Join with a whitespace.
  return pairs.join(" ");
}

document.body.innerHTML = toPairs("38.313072,-89.863845,38.312675,-89.863586,38.310405,-89.862091,38.310405,-89.862091,38.309913,-89.861976,38.309768,-89.861976,38.309768,-89.861976");
&#13;
&#13;
&#13;

答案 4 :(得分:0)

尝试使用while循环,Array.prototype.splice(),将结果作为数组数组返回

&#13;
&#13;
var str = "38.313072,-89.863845,38.312675,-89.863586,38.310405,-89.862091,38.310405,-89.862091,38.309913,-89.861976,38.309768,-89.861976,38.309768,-89.861976"
, res = []
, arr = str.split(",");
while (arr.length) res.push(arr.splice(0, 2))

console.log(res, JSON.stringify(res, null, 2))
&#13;
&#13;
&#13;