用派生字符串javascript替换字符串中的坐标

时间:2014-11-21 01:16:51

标签: javascript regex replace

我尝试取一个文本/字符串,并用替代值替换字符串中的所有坐标。

例如:

This coord is [80,20] and [30,25]

应该成为:

This coord is <a href='location?x=80&y=20'>[80,20]</a> and <a href='location?x=30&y=25'>[30,25]</a> 

基本上,在文本可点击方向链接中建立坐标。

我已经有了我的正则表达式:

/\[\-{0,1}[0-9]{1,}\,\-{0,1}[0-9]{1,}\]/

我已经对它进行了测试,它会选择我正在寻找的内容,但是我不能用javascript替换正则表达式的子字符串,并获取正则表达式的结果,作为替换字符串的一部分。

任何指向正确方向的人都会非常感激:)

3 个答案:

答案 0 :(得分:1)

使用()捕获坐标,然后在替换中将其引用为$1$2等。$&指的是整个正则表达式的匹配。

&#13;
&#13;
var string = 'This coord is [80,20] and [30,25]';
var newString = string.replace(/\[(-?\d+),(-?\d+)\]/g, "<a href='location?x=$1&y=$2'>$&</a>");
alert(newString);
&#13;
&#13;
&#13;

请注意,{0,1}可以简化为?{1,}相当于+。还没有必要在正则表达式中转义=,\d匹配数字。

答案 1 :(得分:0)

尝试使用此

s.replace(
  /\[(\-{0,1}[0-9]{1,})\,(\-{0,1}[0-9]{1,})\]/g, 
  function(match, x, y, offset, str) {
    return  "<a href=\"location?x=" + x +
            "&y=" + y +
            "\">" + "["+x+","+y+"]" + "</a>";
  }
)

答案 2 :(得分:0)

快速回答:

var coordPattern = /\[\s*\+?(-?)\s*0*(\d*\.?\d+)\s*,\s*\+?(-?)\s*0*(\d*\.?\d+)\s*\]/g;
var inputString = "This coord is [80,20] and [30,25]";
var outputString = inputString.replace(coordPattern, "<a href='location?x=$1$2&y=$3$4'>[$1$2,$3$4]</a>");

如果你不关心空间

var coordPattern = /\[\+?(-?)0*(\d*\.?\d+),\+?(-?)0*(\d*\.?\d+)\]/g;

如果你不在乎前面的加号或减号

var coordPattern = /\[\s*()\s*0*(\d*\.?\d+)\s*,\s*()\s*0*(\d*\.?\d+)\s*\]/g;

如果你不关心小数

var coordPattern = /\[\s*\+?(-?)\s*0*(\d+)\s*,\s*\+?(-?)\s*0*(\d+)\s*\]/g;

如果你关心前面的零

var coordPattern = /\[\s*\+?(-?)\s*(\d*\.?\d+)\s*,\s*\+?(-?)\s*(\d*\.?\d+)\s*\]/g;

如果你想要一切!......财富。女性。还有一件事......

var coordPattern = /\[\s*\+?(-?)\s*0*(\d*\.?\d+)\s*,\s*\+?(-?)\s*0*(\d*\.?\d+)\s*\]/g;

测试代码

&#13;
&#13;
var coordPattern = /\[\s*\+?(-?)\s*0*(\d*\.?\d+)\s*,\s*\+?(-?)\s*0*(\d*\.?\d+)\s*\]/g; // If you want it all!...Wealth. Women. And one more thing...
var testStrings = [
    "This coord is [80,20] and [30,25]",
    "This coord is [80 ,20] and [30, 25]",
    "This coord is [ 80,20] and [30,25 ]",
    "This coord is [-80,20] and [+30,25]",
    "This coord is [1.23,20] and [30,4.56]",
    "This coord is [-.123,20] and [+0.456,25]"
];

var expectedAnswers = [
    "This coord is <a href='location?x=80&y=20'>[80,20]</a> and <a href='location?x=30&y=25'>[30,25]</a>",
    "This coord is <a href='location?x=80&y=20'>[80,20]</a> and <a href='location?x=30&y=25'>[30,25]</a>",
    "This coord is <a href='location?x=80&y=20'>[80,20]</a> and <a href='location?x=30&y=25'>[30,25]</a>",
    "This coord is <a href='location?x=-80&y=20'>[-80,20]</a> and <a href='location?x=30&y=25'>[30,25]</a>",
    "This coord is <a href='location?x=1.23&y=20'>[1.23,20]</a> and <a href='location?x=30&y=4.56'>[30,4.56]</a>",
    "This coord is <a href='location?x=-.123&y=20'>[-.123,20]</a> and <a href='location?x=.456&y=25'>[.456,25]</a>"
];

for(var i = 0; i < testStrings.length; ++i){
    var inputString = testStrings[i];
    var expectedString = expectedAnswers[i];
    var outputString = inputString.replace(coordPattern, "<a href='location?x=$1$2&y=$3$4'>[$1$2,$3$4]</a>");

    jQuery('body').append(
      jQuery('<span>').append([
        '--test-passed=' + (outputString === expectedString),
        '--input="' +  inputString + '"',
        '--output=', outputString
        ].join('')
      ),
      jQuery('<br/>')
    );
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;