我需要一个正则表达式来替换Var = xxxx。
ex:index.php?option = xyz
location.replace("--regex to replace option=xyz--","--something--")
好的,我从你的所有答案中找到了更好的解决方案。这是完整的解决方案:
window.location.href.replace(/option=.+&/,'some text')
答案 0 :(得分:1)
window.location.href = window.location.href.replace(/var\=xxxx/, "somevalue");
当然,您需要为要替换的键/值添加正确的正则表达式。
答案 1 :(得分:1)
让我们说你的字符串是这样的:
ticketsSold=30
for(i=0; i<10; i++)
ticketsSold = ticketsSold+1;
alert("total ticketsSold = " + ticketsSold);
允许将其存储在变量中:
var s="ticketsSold=30\nfor(i=0; i<10; i++)\nticketsSold = ticketsSold+1;\nalert("total ticketsSold = " + ticketsSold);"
现在试试他的
alert(s.replace(/[a-zA-z0-9_]+=.+/,'nothing'));
您的输出将是
nothing
for(i=0; i<10; i++)
ticketsSold=ticketsSold+1;
alert('total ticketsSold='+ticketsSold);
我们用“无”替换了ticketSold = 30。