Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using replace and regex to remove characters from this string

I am trying to remove the brackets and 'url' from a string similar to this:

url('http://www.stackoverflow.com');

So it would just leave 'http://www.stackoverflow.com'

The furthest I have got to getting this working is

var myvar = url('http://www.stackoverflow.com');
myvar = myvar.replace(/[url()]/g, '');

But obviously this would mean that any 'u', 'r', or 'l' would be removed from the actual domain.

I guess the answer would be to only remove the first instance of each of the characters.

like image 552
Huxley Avatar asked Mar 24 '26 03:03

Huxley


1 Answers

Use capturing group.

var string = "url('http://www.stackoverflow.com')";
var url =   string.replace(/\burl\('([^()]*)'\)/g, "$1")
document.write(url) 
like image 136
Avinash Raj Avatar answered Mar 26 '26 16:03

Avinash Raj



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!