By default, when we use Replace in ASP, it is case-sensitive. How to make Replace insensitive? Here is a brief introduction for the convenience of friends who need it.
Let’s first take a look at the detailed parameters of Replace
describe
Returns a string in which a specified number of substrings are replaced with another substring.
grammar
Replace(expression, find, replacewith[, compare[, count[, start]]])
The syntax of the Replace function has the following parameters:
Parameter description
expression is required. The string expression contains the substring to be replaced.
find is required. The substring to be searched for.
replacewith is required. The substring to use for replacement.
start optional. The position within expression to begin searching for the substring. If omitted, the default value is 1. Must be used when associated with count
count optional. Number of substring substitutions to perform. If omitted, the default value is -1, meaning all possible substitutions are made. Must be used when associated with start.
compare optional. A numeric value indicating the type of comparison used when evaluating substrings. See the Settings section for values. If omitted, the default value is 0, which means a binary comparison must be performed.
set up
The compare parameter can have the following values:
Constant value description
vbBinaryCompare 0 Performs a binary comparison.
vbTextCompare 1 Performs text comparison.
return value
Replace returns the following values:
If Replace returns
expression is a zero-length string ("").
expression is Null error.
find is a copy of zero-length expression.
replacewith is a copy of the zero-length expression with all content specified by the find parameter removed.
start > Len(expression) Zero-length string.
count is a copy of 0 expression.
illustrate
The return value of the Replace function is the replaced string starting from the position specified by start to the end of the expression string, rather than a copy of the original string from start to end.
The following example uses the Replace function to return a string:
Copy the code code as follows:
DimMyString
MyString = Replace("XXpXXPXXp", "p", "Y") 'Binary comparison starts from the left end of the string. Return "XXYXXPXXY".
MyString = Replace("XXpXXPXXp", "p", "Y", 'Text comparison starts from the third character. Returns "YXXYXXY". 3,, -1, 1)
Method 1: Directly use the replace function that comes with ASP. This is also the simplest method.
title=replace(title,"DF","SD",1,-1,1)
Detailed explanation of replace function parameters:
Parameter 1: source string
Parameter 2: Character to be replaced
Parameter 3: New character. , that is, to replace certain characters in the source string with newly specified characters
Parameter 4: The value is 1. Specifies to search the string starting from the first character
Parameter 5: A value of -1 specifies that each substring must be replaced.
Parameter 6: A value of 1 specifies that string comparisons are case-insensitive.
Two functions for (highlighting keywords)
Method 2: Use regular expressions to replace specified characters without case sensitivity
The following is the function source code:
Copy the code code as follows:
'//Function: string replacement
'//Parameters: regular expression, replaced string, replacement string
Public Function ReplaceTest(patrn, mStr, replStr)
Dim regEx
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
ReplaceTest = regEx.Replace(mStr, replStr)
Set regEx = Nothing
End Function