正则表达式替换特殊字符串

时间:2017-08-10 11:25:50

标签: regex

这是一个网址:http://192.168.84.98/scms?F=image&FORMAT=PNG32&TRANSPARENT=true&LAYERS=show:13&BBOXSR=aaa&IMAGESR=3857&SIZE=2528,867&BBOX=-104100.7571006687,-8544.352197105065,230333.2451006686,106152.75219710357&DPI=90

如何编写一个javascript正则表达式来替换"& BBOXSR ="之间的字符串。和"&"(aaa)到另一个字符串,如" bbb"。请注意搜索字符串(" aaa")是可变的。

希望结果url字符串是:http://192.168.84.98/scms?F=image&FORMAT=PNG32&TRANSPARENT=true&LAYERS=show:13&BBOXSR=bbb&IMAGESR=3857&SIZE=2528,867&BBOX=-104100.7571006687,-8544.352197105065,230333.2451006686,106152.75219710357&DPI=90

1 个答案:

答案 0 :(得分:0)

尽管您不需要正则表达式完成此任务,但这里只是为了REGEX练习而提供解决方案。

var string = " http://192.168.84.98/scms?F=image&FORMAT=PNG32&TRANSPARENT=true&LAYERS=show:13&BBOXSR=aaa&IMAGESR=3857&SIZE=2528,867&BBOX=-104100.7571006687,-8544.352197105065,230333.2451006686,106152.75219710357&DPI=90";

string = string.replace(/(BBOXSR=)(\w)+(?=&)/g, "$1red");
console.info(string)