1

I have a string containing external url links like

.listView a.toggle::after { content: " "; background: url("/test/images/up_small.svg") center center no-repeat; }

I want to grab the text between the url tags like

/test/images/up_small.svg

I tried like

a.split(new RegExp("[^(url)\(\".*\"]"))

but it did not work.

How can I get the external url and if needed replace the same with something else? in JavaScript

1 Answer 1

2

You can search using this regex with 2 capture groups:

(.* url\(")[^"]+(".*)

And replace using $1http://example.com$2 where http://example.com is replacement.

RegEx Demo

Code:

var s = '.listView a.toggle::after { content: " "; background: url("/test/images/up_small.svg") center center no-repeat; }';

var r = s.replace(/(.* url\(")[^"]+(".*)/, '$1http://example.com$2');

console.log(r);

Sign up to request clarification or add additional context in comments.

2 Comments

I tried using s.search(new RegExp(/(.* url\(")[^"]+(".*)/)). It did not give me the correct index though. Also s.split.. does not give me the in between text . I understand replace is working, but extraction how do I do ?
To match URL use: .match(/ url\("([^"]+)"/ and grab capture group #1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.