0

How can i remove style attribute from any tag with regex in asp?

from:

<div style="margin-top:10px;">test</div>

to:

<div>test</div>



Set objRegExp = New regexp
objRegExp.Pattern = "/style\s*=\s*(\'|').+(\'|')/i"
objRegExp.IgnoreCase = True
objRegExp.Global = True
Set resp = objRegExp.Execute(strWordHTML)
For Each respItem In resp
    strWordHTML= replace(strWordHTML,respItem.Value,"")
Next
Set resp = Nothing
Set objRegExp = Nothing

solved *

(\sstyle=['""][^'""]+?['""])
3
  • just trying to modify a code. but it s dont work. Commented Feb 17, 2012 at 10:13
  • in objRegExp.Pattern = "/style\s*=\s*(\'|').+(\'|')/i", the starting / and end /i will be interpreted as part of the pattern so you can remove them. /i is accounted for by objRegExp.IgnoreCase = True Commented Feb 17, 2012 at 10:25
  • 1
    Just what oracle certified professional says, and look at your quote marks. ' does not need to be escaped and is not going to work for " so you have to indicate them. In VBScript " is escaped by a second pair of bunny ears: "". Your regexp pattern string should be something like: "style\s*=\s*[""'].+[""']" Commented Feb 17, 2012 at 15:40

2 Answers 2

2

Not using regex and not tested but something like this should work

str = "<div style=""margin-top:10px;"">test</div>"
start = InStr(str, "style")
first = InStr(start, str, """")
second = InStr(first, str, """")

result = Mid(str, 1, start - 1) + Mid(str, second + 1)
Sign up to request clarification or add additional context in comments.

1 Comment

To escape quotation mark, I think you have to do double quotation mark.
-1
 dim result = Regex.Replace(HtmlText, "style[^>]*", "")

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.