我的字符串有两种 -
var a = /aid/f82eb514073124cd10d468b74eee5663?sg=1#/propertyinfo
或
var a = /aid/f82eb514073124cd10d468b74eee5663#/propertyinfo
我想追加援助之后和之前的内容?或者#" -test"。在上述任一场景中,结果都是f82eb514073124cd10d468b74eee5663-test
因此
a = /aid/f82eb514073124cd10d468b74eee5663-test#/propertyinfo
或
a = = /aid/f82eb514073124cd10d468b74eee5663-test?sg=1#/propertyinfo
答案 0 :(得分:0)
好像你正在寻找像this这样的东西。
正则表达式/\/aid\/[0-9A-F]*/i
和替换表达式$0-test
。
JavaScript与普通的正则表达式滑稽动作略有不同,所以你去吧;
var a = "/aid/f82eb514073124cd10d468b74eee5663?sg=1#/propertyinfo";
alert(a.replace(/(\/aid\/[0-9A-F]*)/i, "$1-test"));

答案 1 :(得分:0)
根据你的例子,我猜/aid/
之后的字符串是某种md5哈希
这应该适合你:
'/aid/f82eb514073124cd10d468b74eee5663#/propertyinfo'.replace(new RegExp('/aid/([a-f0-9]{32})'), '$1-test');
如果您不想对长度有太多具体的了解,可以尝试以下方法:
'/aid/f82eb514073124cd10d468b74eee5663#/propertyinfo'.replace(new RegExp('/aid/([a-f0-9]+)'), '$1-test');
答案 2 :(得分:0)
使用String.replace
函数的简单解决方案:
var a = '/aid/f82eb514073124cd10d468b74eee5663sg=1#/propertyinfo',
result = a.replace(/aid\/([^?#]+)(?=\?|#)/, "aid/$1-test");
console.log(result); // /aid/f82eb514073124cd10d468b74eee5663-test?sg=1#/propertyinfo
答案 3 :(得分:0)
我建议直接替换#
或?
,这样正则表达式很简单。 :)
var a = "/aid/f82eb514073124cd10d468b74eee5663?sg=1#/propertyinfo";
var b = "/aid/f82eb514073124cd10d468b74eee5663#/propertyinfo";
console.log(a.replace(/([\?#])/,"-test$1"));
console.log(b.replace(/([\?#])/,"-test$1"));

答案 4 :(得分:0)
var a = '/aid/f82eb514073124cd10d468b74eee5663?sg=1#/propertyinfo';
a.replace(/(\/aid\/.+)(\?sg=1)(#\/propertyinfo)/,function(text,c,d,e){
return c+'-test'+e;
})
//Output: "/aid/f82eb514073124cd10d468b74eee5663-test#/propertyinfo"
a.replace(/(\/aid\/.+)(\?sg=1#\/propertyinfo)/,function(text,c,d){
return c+'-test'+d;
});
//Output: "/aid/f82eb514073124cd10d468b74eee5663-test?sg=1#/propertyinfo"