A
A
amorphine2017-10-11 17:15:26
bash
amorphine, 2017-10-11 17:15:26

How to escape regex in bash script for sed?

It is required to replace all found occurrences by regular expression with :url()
Regular

/[:,\s]\s*url\s*\(\s*(?:'(\S*?)'|"(\S*?)"|((?:\\\s|\\\)|\\"|\\'|\S)*?))\s*\)/gi

General form:
npm run bla-bla-bla | sed -r "s/[:,\s]\s*url\s*\(\s*(?:'(\S*?)'|\"(\S*?)\"|((?:\\\s|\\\)|\\\"|\\'|\S)*?))\s*\)/:url()/gi" > file.css

I ran into a problem - I can’t screen everything correctly, then I spoil the regular season itself, or something else. Maybe such a complex regex needs awk?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
chupasaurus, 2017-10-11
@chupasaurus

Wrap the regular expression in $'...' with single quote escaping:

npm run bla-bla-bla | sed -r $'"s/[:,\s]\s*url\s*\(\s*(?:'(\S*?)'|\"(\S*?)\"|((?:\\\s|\\\)|\\\"|\\'|\S)*?))\s*\)/:url()/gi"' > file.css

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question