Kiwon Yun
Kiwon Yun

Reputation: 43

JS RegEx to remove part of a URL?

I am using the GoogleBooks API to search for particular titles by name and retrieve a cover image URL. For example, searching for "The Great Gatsby" will return the following image link:

http://books.google.com/books/content?id=HestSXO362YC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api

If you look at the following image, you can see that there is a small fold on the bottom right corner. Some image URLs will have the fold and others won't. If you remove edge=curl from the URL link, the fold is removed.

Is there any way to use a regex to find and delete the curled portion?

Further, is there any way to use regex to change the img=1 value to img=2?

Upvotes: 0

Views: 1551

Answers (4)

Dacre Denny
Dacre Denny

Reputation: 30390

To obtain an updated url where the &edge=curl pattern is replaced and the &img= and &zoom= parameters are updated, you could achieve this by chaining multiple .replace() calls as shown below:

const url = "http://books.google.com/books/content?id=HestSXO362YC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"

// New values for img and zoom parameters
const img = 300;
const zoom = 22;

console.log(
url
.replace(/&img=(\w+)/,`&img=${img}`)    
.replace(/&zoom=\w+/,`&zoom=${zoom}`)
.replace(/&edge=curl/,"")
)

Here the &img= and &zoom= parameters are updated with regular expressions &img=\w+ and &zoom=\w+, where \w+ will match one or more alpha numeric characters that appear after the parameter.

The advantage with this approach (over explicitly specifying img=1 and replacing it with img=2 ) is that you can update those parameter/value substrings of the input url without having to know the actual value of those parameters prior to replacement (ie that img has a value 1).

Note that this approach assumes the parameters being updated are prefixed with & (and not ?).

Hope that helps!

Upvotes: 1

Salman Arshad
Salman Arshad

Reputation: 272386

Don't use regex to parse URLs. Use URL object:

var u = new URL("http://books.google.com/books/content?id=HestSXO362YC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api");

u.searchParams.delete("edge");
u.searchParams.set("img", "2");

console.log(u.href);

Upvotes: 1

Ahmed HENTETI
Ahmed HENTETI

Reputation: 1128

try this

let url = 'http://books.google.com/books/content?id=HestSXO362YC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api';
// remove &edge=curl
url = url.replace('&edge=curl', '');
// replace img=1 with img=2
url = url.replace('img=1', 'img=2');

Upvotes: 0

Ahmed Gaafer
Ahmed Gaafer

Reputation: 1671

you can use the .replace() method

let URL = "some random URL you have"
console.log(URL.replace('&edge=curl',''))

Will replace every "&edge=curl" that it finds in this string and replace it with '' an empty string which is basically removing it.

You can also use the same method .replace() to replace any static URL variables like "img=1"

console.log(URL.replace('img=1','img=2'))

Upvotes: 1

Related Questions