views:

67

answers:

2

hi anyone knows how to replace a html tag in a string with specific characters: e.g.

string s1 =  "<span style="italic">inluding <span style="bold">other</span> tags </span>";
string s2 =  "<span style="italic">inluding </span><span style="bold">other tags </span>";

i want to replace "span" with "bold" to "bOpen" and "bClose" and to replace "span" with "italic" to "iOpen" and "iClose" in both c# and javascript. thanks very much.

thanks for the response, i did use regular expression to do that: res = Regex.Replace(res, ".*?", replaceHtmlBold); but it cant match the nested tag and none-nested tag at the same time. could you help please?

A: 

JavaScript's String Object has a handy function that lets you replace words that occur within the string. So does C#.

twodayslate
You have missunderstood the point.
Sergio
A: 

Regular expressions are your friends here. I could give you the exact code for your problem, but then you'll miss the point of learning this technique. Here is an Introduction to Regular Expressions and there is this article "C# Regular Expressions". If you need more, Google is your friend.

Good luck!

PS: I realized now what the real problem is. I think you can get away with lookaround techniques and conditionals. Both are summarized here.

Sergio
thanks for the response, i did use regular expression to do that:res = Regex.Replace(res, "<span style=\"font-weight: bold;\">.*?</span>", replaceHtmlBold);but it cant match the nested tag and none-nested tag at the same time. could you help please?
sam
I have added some comments above. Now I have to go home. I'll think about it and tell you later.
Sergio