1

I have a telerik RadEditor that a user is able to enter HTML into an editor and save to my database. Most of the time this works great, but I'm having a problem with certain instances when there is a css attribute of position: absolute; or z-index: 100; (could be any # for z-index) within the element's style attribute.

I looked at the telerik options and don't see an out of the box solution (correct me if I'm wrong) but I am basically trying to find a Regex that I can use to strip out those CSS properties in the code behind. This project uses VB.Net (I am a C# developer) so I'm already fighting an uphill battle. I originally was looking at this example from stack but I'm not exactly trying to do the same thing and I am no master of writing my own Regexes...

Here's what I have so far that does not work as expected:

Dim html As String = "<div style=""position: absolute; z-index: 6;"">a bunch of other html</div>"

html = Regex.Replace(html, "((?:position|z-index)(?:[^:]+):(?:\\s*))([^;]+)", "")

I don't know if I just have a small syntax issue, or if I'm completely off in my approach...

Please note, I need to remove the properties with or without a semicolon and ideally should ignore a space between the colon, so these should all get removed:

  • position: absolute;
  • position : absolute;
  • position: absolute
  • position : absolute
  • z-index: anyInt;
  • z-index : anyInt;
  • z-index: anyInt
  • z-index : anyInt

The only other requirement is that I to strip out ALL occurrences, not just the first. Any help with solving this issue will be greatly appreciated!

3 Answers 3

2

If i understand:

(?:position|z-index)[ ]*:[ ]*[^;]+;

It remove all properties position and z-index

Or:

(?:position|z-index)\s*:\s*(?:absolute|\d+);?
Sign up to request clarification or add additional context in comments.

2 Comments

This is almost what I need as the position and z-index along with their values are removed, however the semicolons are getting left behind, such as '<div style="some attribute: value; ; another-attribute: value; ;">'
Ahh, nevermind, I missed the semicolon at the end of your example... this works, marking as the correct response!
1

Try Something like this:

html = Regex.Replace(html, "(position|z-index)\\s*:\\s*(\\w|\\d)+\\s*;?", "", RegexOptions.IgnoreCase | RegexOptions.Multiline);

You'll need to use RegexOptions.Multiline in order to replace all occurrences of the pattern. I used RegexOptions.IgnoreCase so the Regex engine will replace the occurrences independent of their letter case.

Hope it helps.

Comments

0

Seems like you could simplify somewhat, and make sure you are only getting instances like "position:absolute" or "z-index:int", rather than also matching other possible values for the position and z-index attributes:

html = Regex.Replace(html, "((position\\s*:\\s*absolute;?)|(z-index\\s*:\\s*\\d+;?))", "")

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.