ASP highlight case-insensitive keywords

Source: Internet
Author: User

For example, for text abcabcabcabcabcabcabca, the keyword BC, there are 6 matching items in case of case insensitive.
Abcabcabcabcabcabcabca is displayed on the webpage.
Many people think of the replace function. Prototype:
Replace (string, find, replacewith [, start [, Count [, compare])
String mandatory, string expression, including the substring to be replaced
Find (required): The substring to be searched.
Replacewith: A required substring to be replaced.
Start: Optional. Start to search for the position of the substring. The default value is 1.
Count option. The number of substrings to replace. The default value is-1, indicating all possible replications.
Compare option, comparison method, 0: Binary comparison; 1: text comparison

Although the last parameter can solve the case-insensitive problem, why should it be replaced?
In this example, BC, and BC are all searched, but cannot be replaced with a single text.
Use the instr function to help us.
Search from the source string, left to right, and each matching item is found. Take three steps
1. output the string on the left of the matching item
2. Set the matching item to the style <span> and output it.
3. Repeat the previous two steps to continue searching for the string on the right until the end of the search.
Code As follows: Copy code The Code is as follows: public function highlight (S, F)
Dim TL, TM, TR, K
TL = ""
TM = ""
Tr = s
K = instr (1, TR, F, 1)
Do while K> 0
TL = TL & left (TR, k-1)
TM = mid (TR, K, Len (f ))
TL = TL & "<span style = 'color: red'>" & TM & "</span>"
Tr = right (TR, Len (TR)-len (f)-k + 1)
K = instr (1, TR, F, 1)
Loop
Highlight = TL & tr
End Function

The Code is as follows:
Copy codeThe Code is as follows: TS = "abcabcabcabcabcabcabca"
TF = "BC"
Response. Write (TS)
Response. Write ("<br/> ")
Response. Write (highlight (TS, TF ))

This allows you to start an instance.
On the other hand, consider whether regular expressions are more convenient? Failed after several attempts. Let's see which expert uses regular expressions to solve this problem.
after writing the Article , the netizen "yugong" provides a solution to the regular expression. The test result is correct. Now he posts his code to the back. Thank you very much.
Code copy Code the code is as follows: function highlight (S, F)
dim RegEx
set RegEx = new Regexp
RegEx. ignorecase = true
RegEx. global = true
RegEx. pattern = "(" & F & ")"
Highlight = RegEx. replace (S, " $1 ")
end function
response. write highlight ("abcabcabcabcabcabcabca", "BC")

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.