[英]Find and replace any number in href value with Javascript
这是我当前的代码,不起作用:
$( "#cart_url" ).attr('href', this.href.replace( "ProductCode=TFLG1&Qty.TFLG1="/^[0-9]+$/"&", "test" ));
我不确定如何编写语法。 在我有“ / ^ [0-9] + $ /”的地方,我只是想用“ test”替换任何数字的结果。
更多信息:
$( "#cart_url" ).attr('href', this.href.replace( "ProductCode=TFLG1&Qty.TFLG1=345&", "test" ));
这段代码可以正常工作,但是数字“ 345”可以是1-1000,因此我需要动态查找并替换该数字,而不是将此函数编写1000次。
如果我理解正确,则所需的模式类似于/[Qty.TFLG1][\\d]+(?=&)/g
它将以这种方式工作:
var pattern =/[Qty.TFLG1][\d]+(?=&)/g;
$("#cart_url").attr("href", this.href.replace( pattern, "test" ));
那将返回类似:
ProductCode=TFLG1&Qty.TFLG1=test&
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.