开发者

Regular expression to replace Var=xxxx in Javascript [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and开发者_JS百科 cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜