thatuxguy
thatuxguy

Reputation: 2528

replace characters with regex

Morning,

I need to replace some characters in jQuery with different characters due to the way images are displayed on the site. I need to replace the & and the / with an _

I currently have, how would i do the / ?

$('#lnkProdImage').attr("href", "http://img.website.com/500/" + res.sku.replace(/&/g, '_').replace(/[\s]/g, '_') + ".jpg");

Many thanks in advance.

Upvotes: 0

Views: 129

Answers (2)

Madara's Ghost
Madara's Ghost

Reputation: 174937

Seriously?

$('#lnkProdImage').attr("href", "http://img.website.com/500/" + res.sku.replace(/[&\s\/]/g, '_') + ".jpg");

Why the 3 replaces? Could be done in a single one!

Upvotes: 1

Anton Kucherov
Anton Kucherov

Reputation: 307

$('#lnkProdImage').attr("href", "http://img.website.com/500/" + res.sku.replace(/&/g, '_').replace(/[\s]/g, '_').replace(/\//g, '_') + ".jpg");

Upvotes: 0

Related Questions