Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi All,
I want to match words from my database and for that i use regular expression to ignore html tags my expression is "
<script.*/*>|</script>|<.[^>]*>
". But it dont handle words comes with apostrophe like Dont't, It's and so on.

Below is the Method that i am using to return the words

XML
private string HighlightSearchKeyWords(string TextToHighlight, string TextAsDiscription, string text)
        {
            string ProcessedText = text;
            var pattern = keywordPattern(TextToHighlight);
            
            string TagExpression = "<script.*/*>|</script>|<.[^>]*>";
            Regex reg = new Regex("(" + TagExpression + ")|(" + pattern + ")", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            MatchReplacement = "<span id='" + TextToHighlight.ToString() +"_Span"+ "' style='background-color: yellow; cursor: hand;' onmouseover='javascript:SaveMouseOVerWords(this,this.id)' onmouseout='javascript:HideContent()' mytitle='" + TextAsDiscription + "'>$1</span>";//onmouseout='javascript:HideContent()'
            ProcessedText = reg.Replace(ProcessedText, new MatchEvaluator(MatchEval));
            return ProcessedText;
        }


But this method return the words with apostrophe also and i want ignore those words
Please suggest what should add in my existing expression so that he handle apostrophe as well.

NOTE:
My problem is i have read all the browser content in a string and now i am trying match list of words with the browser content, when the word match i highlight that word, but word like "abstract or absolute" which come with style tag also get highlight. i want to escape those words that come inside HTML style tag. In order to escape all tag inside script tag i have written "<script.*/*>|</script>|<.[^>]*>" . so i want to add someting in the existing regex expression to escape all style tag.


Waiting for your response.

Thanks
AP
Posted
Updated 20-Feb-13 21:53pm
v3
Comments
Sergey Alexandrovich Kryukov 18-Feb-13 14:10pm    
In this regex, you do nothing related to word characters, no you use apostrophe, so what do you want?
—SA
Member 4531085 21-Feb-13 3:53am    
Hi, My problem is i have read all the browser content in a string and now i am trying match list of words with the browser content, when the word match i highlight that word, but word like "abstract or absolute" which come with style tag also get highlight. i want to escape those words that come inside HTML style tag. In order to escape all tag inside script tag i have written "<script.*/*>|</script>|<.[^>]*>" . so i want to add something in the existing regex expression to escape all style tag.

1 solution

There is no obvious reason why the regex you have given us would not work with apostrophes - all it does is look for tags, and ignore text. So unless you use the regex to replace the tags with blanks, the output is very unlikely to contain "don't", "it's" and so on anyway.

Are you sure it's the regex that's the problem? Normally a problem with databases and apostrophes is caused by concatenating strings to form an SQL query instead of using parametrized queries, where the attempt to insert or update a row fails with an SQL error message (or in extreme cases deletes your database).

Check the actual data that you are feeding to the DB, and check how you are doing it. I suspect that the regex is a red herring!
 
Share this answer
 
Comments
Member 4531085 21-Feb-13 3:52am    
Hi, My problem is i have read all the browser content in a string and now i am trying match list of words with the browser content, when the word match i highlight that word, but word like "abstract or absolute" which come with style tag also get highlight. i want to escape those words that come inside HTML style tag. In order to escape all tag inside script tag i have written "<script.*/*>|</script>|<.[^>]*>" . so i want to add someting in the existing regex expression to escape all style tag.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900