0

Is there an easy way to change a string like "DIV class=demo id=3" to: "DIV class='demo' id='3'"?

I am planning to write a recursive function. Is there any easy way to do this?

Laurel
  • 5,965
  • 14
  • 31
  • 57
balaG
  • 431
  • 1
  • 8
  • 16
  • 1
    Do you want to add single quotes around every unquoted value or just `class` and `id`? – Abe Miessler Aug 24 '11 at 16:58
  • Hi there! Never use regex to parse html. Everybody on SO is going to hate you otherwise. see this: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags good luck :) – user194076 Aug 24 '11 at 17:02
  • Yes Abe, want to add single quotes around every unquoted value – balaG Aug 24 '11 at 17:20

3 Answers3

1
result = Regex.Replace(input, @"(\b\w*=)(\w*\b)", @"$1'$2'");

This replaces any alphanumericword=alphanumericword with alphanumericword='alphanumericword'

But this might not be exactly what you want.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Casperah
  • 4,504
  • 1
  • 19
  • 13
0

find this

/(<[^>]+?=)([^"'\s][^\s>]+)/gi

replace with this

$1'$2'

note this has worked for years for me until youtube started putting white space in their links!! Bad youtube!!

Tom McDonald
  • 1,532
  • 2
  • 18
  • 37
0

Since title has nothing to do with question I'd advice unrelated thing too :)

Do not use IE7 mode for your web page and you will get innerHTML formatted properly the way you expect.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Actually i am getting this data from a service. But i need to load to the XMLDocument where it should be properly formatted with quotes. Anyway thanks. – balaG Aug 24 '11 at 17:13
  • Your answer is being discussed on meta [here](http://meta.stackoverflow.com/questions/321395/what-should-i-do-with-answers-that-try-to-solve-the-problem-without-answering-th) – rene Apr 20 '16 at 16:39