Find and Replace in HTML Text

Asked By Soft DEveloper
07-Nov-09 06:04 AM
Earn up to 0 extra points for answering this tough question.

Hello,

I have HTML Text along with HTML tags.I want to find a particular word and replace it with other

Problem is if HTML Text contains text as "Cold,<strong>Cough</strong>,Fever,Para"

 And if i want to remove "Cough," then comma(,) is after strong closing tag (</strong>).

As in above case i have made Cough bold, user can do any HTML formatting so how can I remove particular 

word with comma.

  Find and Replace in HTML Text

mv ark replied to Soft DEveloper
07-Nov-09 07:21 AM
Use the line below to remove any HTML tags from the content -
string strippedText = System.Text.RegularExpressions.Regex.Replace(text,@"<(.|\n)*?>",string.Empty);
You can then find & replace the required word from the variable strippedText

  Find and Replace in HTML Text

Soft DEveloper replied to mv ark
07-Nov-09 07:49 AM
But after replacing I want to keep formaating if done on any other word then How can I do this?

  re

Web Star replied to Soft DEveloper
07-Nov-09 10:49 AM

whats problem if u wna to replace any word with comma(,)

string strippedText = System.Text.RegularExpressions.Regex.Replace("Cough",",");

rest format will be same where user done

  Highlight Text Using Javascript
F Cali replied to Soft DEveloper
07-Nov-09 11:50 PM

If I understand your requirement correctly and based on your example, you want to highlight certain words in your HTML page.  This is similar to what other web pages do when they are found from a search engine like google.  If this is the case, the following links may be able to help you do just that:

http://www.eggheadcafe.com/articles/highlight_google_keywords.asp

http://eriwen.com/javascript/highlight-search-results-with-js/

Regards,
SQL Server Helper

  Find and replace in HTML TEXT
Soft DEveloper replied to F Cali
08-Nov-09 12:20 AM

Hello,

I have HTML Editor in my Windows application in which user can type and fomat their text such as they can make their text bold,Change Font,Text Color etc.I also have a grid in which User can select from the grid.

Whenever User selects record from the grid,the text from grid is added in my HTML Editor appending text with comma. i.e if User selects Cough and Cold from grid the text displayed in HTML Editor is

Cough,Cold.

Now after adding this text user can do various formatting as described above.

If the User makes "Cough" word BOLD then I have value as <strong>Cough</strong>,Cold.

Now if User deselects the cough row from grid it should be removed from HTMLEditor So now how can I find "Cough," so that the string remaining in HTML Editor is only "Cold" and not ",Cold"

and in my database I have stored text along with HTML tags.

  Replace
F Cali replied to Soft DEveloper
08-Nov-09 12:29 AM

One option you can do is to continue with your replacing of the string without the comma.  Then follow that replace with another replace and check if the remaining string starts with a comma and if it does, then remove that extra comma.  If the text that was replaced is in the middle of the selection, then look for two consecutive commas and replace it with a single comma.

So for case #1, "<strong>Cough</strong>,Cold" becomes ",Cold".  Since it starts with a comma, replace that first comma with an empty string.

And for case #2, "Flu,<strong>Cough</strong>,Cold" becomes "Flu,,Cold".  Since there's 2 commas together, replace that with a single-comma.

Hope this helps.

Regards,
SQL Server Helper

  About HTML text
Soft DEveloper replied to F Cali
10-Nov-09 05:14 AM

Case 2nd which you have told will not contain string as "Flu,,Cold" but the bold tag which was formatted for "Fever" will also be present in that string i.e the string will be "Flu,<stromg></strong>,Cold.

So how to parser and write generic code for a string which contains textalong with tags an d replace it?

Create New Account