【问题标题】:Reconstruct array重构数组
【发布时间】:2016-04-22 23:35:35
【问题描述】:

我有数千行 Javascript 这样的代码:

var myCoordinates = [

    new google.maps.LatLng(11.11111,22.222222),
    new google.maps.LatLng(33.33333,44.444444),
    new google.maps.LatLng(55.55555,66.666666),

    //thousands of lines
];

我需要将上面的代码格式化为不带空格的代码(也就是我可以复制到我的 PHP 代码的字符串)


[["lat"=>11.11111,"lng"=>22.22222],["lat"=>33.33333,"lng"=>44.44444],["lat"=>55.55555,"lng"=>66.66666],/*etc*/];

我尝试过但失败了:

document.getElementById('format-submit').onclick = function() {

    var textareaValue = document.getElementById('format-textarea').value;

    var findA = 'var myCoordinates = ';
    var rA = new RegExp( findA, 'g' );
    var resultA = textareaValue.replace( rA, '' );

    //I get this error on this line:

    //Uncaught SyntaxError: Invalid regular expression: /new google.maps.LatLng(/: Unterminated group

    //              |
    //              V

    var findB = 'new google.maps.LatLng(';
    var rB = new RegExp(findB, 'g');
    var resultB = resultA.replace(rB, '["lat" => ');

    var findC = '),';
    var rC = new RegExp(findC, 'g');
    var resultC  = resultB.replace(rC, '],');

    var findD = ')';
    var rD = new RegExp(findD, 'g');
    var finalResult  = resultC.replace(rD, ']');

    textareaValue = finalResult;
};

速度或性能不是问题,我只需要:

  1. 复制第一个JS代码到textarea
  2. 按下按钮时格式化
  3. 用格式化代码替换 textarea 值
  4. 复制它
  5. 将其粘贴到我的代码中

问题:

如何替换字符串中包含各种字母、特殊字符等的部分?

(可能是错误原因,对吧?)

【问题讨论】:

  • 正则表达式具有特定的语法,将带有特殊含义的字符的字符串发送到 RegExp 构造函数将导致错误。您应该只使用字符串本身(例如resultB.replace(findC, '],');

标签: javascript php arrays


【解决方案1】:

试试这个代码,我猜它会给你你想要的

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
var myCoordinates = [

    new google.maps.LatLng(11.11111,22.222222),
    new google.maps.LatLng(33.33333,44.444444),
    new google.maps.LatLng(55.55555,66.666666),

    //thousands of lines
];
var fullJson = [];
for(var i = 0; i < myCoordinates.length; i++) {
    var temp = {};
    var pos = myCoordinates[i];
    temp['lat'] = pos.lat();
    temp['lng'] = pos.lng();
    fullJson.push(temp);
} 
var str_json = JSON.stringify(fullJson);
$.post('index.php', str_json, function(result) {
    console.log(result);
});
</script>

<?php
if (isset($_POST)) {
    $str_json = file_get_contents('php://input');
    echo '<pre>';
    print_r($str_json);
    echo '</pre>';
}

【讨论】:

    【解决方案2】:

    我相信这就是你要找的。​​p>

    document.getElementById('format-submit').onclick = function() {
        var textarea = document.getElementById('format-textarea');
    
        textarea.value = textarea.value
            .replace(/^[^\[]+/,'')
            .replace(/^.*\(([\d.]+),([\d.]+)\)(,)?$/gm,'["lat" => $1, "lng" => $2]$3')
            .replace(/\s+/g,'');
    };
    <textarea id="format-textarea" rows="7" cols="46">var myCoordinates = [
    
        new google.maps.LatLng(11.11111,22.222222),
        new google.maps.LatLng(33.33333,44.444444),
        new google.maps.LatLng(55.55555,66.666666)
    
    ];</textarea><br>
    <input type="button" id="format-submit" value="Format textarea">

    【讨论】:

    • 谢谢,这似乎可以解决问题。 您能否修改您的答案以同时删除所有空格?
    • @Solo Edited,这是你想要的吗?
    • 差不多。有没有办法删除所有个空格? =&gt;commas 周围也是如此。
    猜你喜欢
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    相关资源
    最近更新 更多