Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Replace part of string using a variable

Tags:

jquery

replace

This one seems simple, but I have no idea why it's not working. I would like to replace part of a string in a variable.

Say, I have this code:

var url = "At vero eos et accusam et justo duo dolores et ea rebum.";
url.replace('At vero eos et accusam et ', '');

Why doesn't it work? This one works:

var url = "At vero eos et accusam et justo duo dolores et ea rebum.".replace('At vero eos et accusam et ', ''):

-- but I'm using the variable elsewhere, so I would like to keep the replace out of the variable definition.

Fiddle here.

like image 665
Meek Avatar asked Jan 01 '26 09:01

Meek


2 Answers

You need to save the replaced string

  var url = "At vero eos et accusam et justo duo dolores et ea rebum.";
  url = url.replace('At vero eos et accusam et ', '');
//^^^^^this

DEMO

like image 59
Anton Avatar answered Jan 03 '26 22:01

Anton


Updated FIDDLE

     var url = "At vero eos et accusam et justo duo dolores et ea rebum.";
     url=url.replace('At vero eos et accusam et ', '');
     $('#box').text(url);
like image 29
P.Sethuraman Avatar answered Jan 04 '26 00:01

P.Sethuraman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!