Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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
 
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 18-Feb-13 5:36am
Edited 20-Feb-13 21:53pm
v3
Comments
Sergey Alexandrovich Kryukov at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
Comments
Member 4531085 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 343
1 _Amy 290
2 Manfred R. Bihy 200
3 Maciej Los 185
4 CHill60 180
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,347
2 Maciej Los 3,799
3 Peter Leow 3,558
4 CHill60 2,702


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100