Make replace case insensitive in Classic ASP (Vbscript)

Asked

Viewed 348 times

2

I have the string below:

texto = "Meu texto"

I would like to replace the word "My" and return it in bold. The result would look like this:

Man text

However, replace is case sensitive, that is, if I use replace(texto,"meu","<b>meu</b>") nothing happens because of the capital’M' .

Is there any way I can replace in classic ASP with Vbscript ignoring upper or lower case letters? Or it’s easier to do this in Javascript or jQuery?

Note: I could convert everything to lowercase, but I want the return to keep the uppercase letters of the string.

1 answer

1


The syntax of Replace of ASP VBScript is the following:

Replace(string,find,replacewith[,start[,count[,compare]]])

where the last parameter compare must be placed 1 which is the text search (Explanation: 1 = vbTextComparate string comparison in Textual form. (Insensitive)) whereas the standard is 0 which is the binary research, therefore the amendment is not made.

Example:

<%

    Dim texto
    texto = "Meu texto"
    
    Response.Write ( replace(texto,"meu","<b>Meu</b>", 1, -1, 1) )
    Response.Write ( "<br />")
    Response.Write ( replace(texto,"mEu","<b>Meu</b>", 1, -1, 1) )
    Response.Write ( "<br />")
    Response.Write ( replace(texto,"meU","<b>Meu</b>", 1, -1, 1) )
    Response.Write ( "<br />")
    Response.Write ( replace(texto,"MEU","<b>Meu</b>", 1, -1, 1) )
    Response.Write ( "<br />")    

%>

Exit:

Man text

Man text

Man text

Man text

References

  • 1

    Thank you! It worked as I would like.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.