str.replace()

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Posts: 73
Joined: Wed Dec 14, 2022 1:13 pm

str.replace()

Post by AZJIO »

Using ReplaceString() to add the highlight tags, the text "KV" is replaced with "kv" if I searched in lower case. I need to preserve the case of the letters, so I use a regular expression to get the "$1" group in the original case. The example taken from the javascript is working as I have used it before, but I can't get it to work in this code. I'm using Russian letters, since that might be a problem, I did a number search and it still doesn't work.

Code: Select all

If g_Highlight And Not g_rehght
	g_rehght = CreateRegularExpression(#PB_Any, "(.*?)(" + SearchAll$ + ")(.*?)", #PB_RegularExpression_AnyNewLine | #PB_RegularExpression_NoCase)
EndIf

; MenuItem(ListSize(LFiles()) - 1, ReplaceString(LFiles()\title, SearchAll$, "<font color=#89F>" + SearchAll$ + "</font>", #PB_String_NoCase))
name2$ = LFiles()\title
; !v_found = v_g_rehght.test(v_name2$)
; !v_name2$ = v_name2$.replace(v_g_rehght, "<font color=#89F>$1</font>");
; Debug g_rehght
; !v_name3$ = v_name2$.replace(v_g_rehght, "$1<font color=#89F>$2</font>$3");
; !v_name2$ = v_name2$.replace(/(v_searchall$)/i, "<font color=#89F>$1</font>");
; !v_name2$ = v_name2$.replace(/^(.*?)(v_searchall$)(.*?)$/im, "$1<font color=#89F>$2</font>$3");
re2$ = "/(" + SearchAll$ + ")/i"
!v_name3$ = v_name2$.replace(v_re2$, "<font color=#89F>$1</font>");
; Debug name2$
MenuItem(ListSize(LFiles()) - 1, name3$)
I got this to work, expecting you to ask why I don't use ReplaceRegularExpression...

Code: Select all

name3$ = ReplaceRegularExpression(g_rehght, name2$, "$1<font color=#89F>$2</font>$3")
But I'm still wondering what's wrong with my code str.replace(regexp, replace)