Regular expression to replace Var=xxxx in Javascript [closed]
I need a regular expression to replace Var=xxxx with something.
ex: index.php?option=xyz
location.replace("--regex to replace option=xyz--","--something--")
Ok I found better solution from all of your answers. Here is complete solution:
window.location.href.replace(/option=.+&/,'some text')
window.location.href = window.location.href.replace(/var\=xxxx/, "somevalue");
Of course, you'll want to put the right regular expression for the key/value you want to replace.
Lets say your string looks like this:
ticketsSold=30
for(i=0; i<10; i++)
ticketsSold = ticketsSold+1;
alert("total ticketsSold = " + ticketsSold);
lets store that in a variable:
var s="ticketsSold=30\nfor(i=0; i<10; i++)\nticketsSold = ticketsSold+1;\nalert("total ticketsSold = " + ticketsSold);"
now try out his
alert(s.replace(/[a-zA-z0-9_]+=.+/,'nothing'));
your output will be
nothing
for(i=0; i<10; i++)
ticketsSold=ticketsSold+1;
alert('total ticketsSold='+ticketsSold);
we have replaced ticketsSold=30 with "nothing".
精彩评论