简体   繁体   中英

Replace a word in a string using JQuery

I've tried using replaceAll and replaceWith and they don't seem to work

for example, string

"~/Foo.aspx?fn=/image.jpg&p=True"

I want to do is replace p=True with p=False

var previewSource = "~/Foo.aspx?fn/image.jpg&p=True"
var loadedSource = $(previewSource).replaceAll("p=True").replaceWith("p=False");// This is one of the things I have tried.

You can use pure Javscript here:

var previewSource = '~Foo.aspx?fn/image.jpg&p=True';
var loadedSource = previewSource.replace("p=True", "p=False");

How about:

var previewSource = "~Foo.aspx?fn/image.jpg&p=True";
var loadedSource = previewSource.replace(/p=true/i, 'p=False'); // This is one of the things I have tried.

PS

Since the value of previewSource is string, you need to wrap them in quotes. Then use simple javascript to replace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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