0

Alright, so I've been looking around for quite a while trying to figure out how to get this to work out. So what I'm trying to do is replace anything in strings that looks like this:

foo: bar;

But only if its not inside something like this:

<div style='foo: bar; ofoo: obar'>

So the basic idea is that I want to replace css when its not inside html style attributes. I understand that you can use a for loop and check it but I would like to do this with just the regex replace.

I'm using JavaScript Regex heres what my code attempt currently looks like:

\b(.*?):(|\s)(.*?);

https://regex101.com/r/LWohvu/1

Notes: I understand that you could use a ^ to check if it starts with it but that only works for the first line.

If I didn't cover any needed any information please feel free to comment!

14
  • 1
    Don't use regex to parse HTML even it concerns getting CSS out of it. Commented Apr 6, 2017 at 14:28
  • 1
    Are the preset requirements for the html element styles in the first place? It would be far easier to add and remove classes. Is it custom user styling or something like that being added at runtime? If that's the case you could add those custom styles to classes in a <style> section in the head on page load and set an !important attribute on them or something like that. Commented Apr 6, 2017 at 14:29
  • m modifier makes ^ work for every line. Commented Apr 6, 2017 at 14:30
  • 1
    Also, @trincot is right. While you 'can' parse HTML using regex, there are a lot of reasons not to. The link posted to the previous SO answer is one of the most popular SO answers of all time for a reason, and it isn't just because it's funny. Commented Apr 6, 2017 at 14:49
  • 1
    Well, I still don't understand why you don't want to change the style that's already there with the full length aggregated from the divs, but you could just add to it with JS doing something like theStyledElement.style.cssText = (theStyledElement.style.cssText + theStyleDiv.innerHTML); Edit: In this case I'm assuming the innerHTML will just be the text the user types in. Commented Apr 6, 2017 at 15:10

1 Answer 1

2

According to your description, you want to replace all style in your html page except those are inside of a html tag. I've updated your regex and this worked according to your need. Please check this.

Regex:

^(?!(\=|\<))(.*?):(.*?);

Regex in JavaScript:

/^(?!(\=|\<))(.*?):(.*?);/gm

All style start with style= if this exists inside of a html tag. So, I've tried to avoid those using ^(?!(\=|\<)). This represent not start with = and <. Avoid = for style and < for html tag.

Please check this in Updated Regex.

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

Comments

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.