简体   繁体   English

查找并用Javascript替换href值中的任何数字

[英]Find and replace any number in href value with Javascript

This is my current code, which does not work: 这是我当前的代码,不起作用:

$( "#cart_url" ).attr('href', this.href.replace( "ProductCode=TFLG1&Qty.TFLG1="/^[0-9]+$/"&", "test" ));

I'm not exactly sure how to write the syntax. 我不确定如何编写语法。 Where I have "/^[0-9]+$/" , I'm just trying to replace any result of any number with "test". 在我有“ / ^ [0-9] + $ /”的地方,我只是想用“ test”替换任何数字的结果。

Further information: 更多信息:

$( "#cart_url" ).attr('href', this.href.replace( "ProductCode=TFLG1&Qty.TFLG1=345&", "test" ));

This code would work fine, but the number '345' could be anything from 1-1000, so I need to find and replace that number dynamically instead of writing this function 1000 times. 这段代码可以正常工作,但是数字“ 345”可以是1-1000,因此我需要动态查找并替换该数字,而不是将此函数编写1000次。

If I understood correctly, the pattern you need is something like /[Qty.TFLG1][\\d]+(?=&)/g 如果我理解正确,则所需的模式类似于/[Qty.TFLG1][\\d]+(?=&)/g

It would work this way: 它将以这种方式工作:

var pattern =/[Qty.TFLG1][\d]+(?=&)/g;
$("#cart_url").attr("href", this.href.replace( pattern, "test" ));

That would return something like: 那将返回类似:

ProductCode=TFLG1&Qty.TFLG1=test&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM