Zwykle zastępując wszystkie wystąpienia pasujących ciągów, pochodzących z innych języków programowania, String.replace()
zwykle zastępuje wszystkie wystąpienia pasujących ciągów. Jednak nie jest tak w przypadku javascript/typescript. Znalazłem wiele rozwiązań w Internecie z javascript z wykorzystaniem regex. Natychmiast miałem problemy z tym rozwiązaniem ze względu na specjalne znaki. Podejrzeń, że istnieje sposób, aby poprawić to za pomocą regex, ale nie jestem ekspertem od regex. Jak wielu zrobiło przede mną, stworzyłem własną metodę.W maszynopisie String.Replace zastępuje tylko pierwsze wystąpienie dopasowanego ciągu. Need String.replaceAll() metoda
Być może istnieją sposoby na poprawę wydajności, korzystając z niestandardowej klasy StringBuilder()
. Witam wszelkie myśli.
public static Replace = function (originalString: string, oldValue: string, newValue: string, ignoreCase: boolean = false) {
//
// if invalid data, return the original string
//
if ((originalString == null) || (oldValue == null) || (newValue == null) || (oldValue.length == 0))
return (originalString);
//
// do text replacement
//
var dest = "";
var source: string = originalString;
if (ignoreCase)
{
source = source.toLocaleLowerCase();
oldValue = oldValue.toLowerCase();
}
//
// find first match
//
var StartPos = 0;
var EndPos = source.indexOf(oldValue, StartPos);
var Skip = (EndPos >= 0) ? EndPos - StartPos : source.length-StartPos;
//
// while we found a matched string
//
while (EndPos > -1) {
//
// copy original string skipped segment
//
if (Skip > 0) dest += originalString.substr(StartPos, Skip);
//
// copy new value
//
dest += newValue;
//
// skip over old value
//
StartPos = EndPos + oldValue.length;
//
// find next match
//
EndPos = source.indexOf(oldValue, StartPos);
Skip = (EndPos >= 0) ? EndPos - StartPos : source.length - StartPos;
}
//
// append the last skipped string segment from original string
//
if (Skip > 0) dest += originalString.substr(StartPos, Skip);
return dest;
}
W celu dodania wsparcia dla tej metody do klasy string
I dodaje następujący kod:
interface String { EZReplace(oldValue: string, newValue: string, ignorCase?: boolean): string; }
String.prototype.EZReplace = function (oldValue: string, newValue: string, ignorCase: boolean = false) {
return EZUtil.Replace(this, oldValue, newValue, ignorCase);}
.... Po ponownym oglądania innych stanowisk, to zmodyfikowany kod do korzystania z regularnych wyrażenia. Interesujące byłoby wykonanie testów wydajności.
public static Replace = function (originalString: string, oldValue: string, newValue: string, ignoreCase: boolean = false) {
//
// if invalid data, return the original string
//
if ((originalString == null) || (oldValue == null) || (newValue == null) || (oldValue.length == 0))
return (originalString);
//
// set search/replace flags
//
var Flags: string = (ignoreCase) ? "gi" : "g";
//
// apply regex escape sequence on pattern (oldValue)
//
var pattern = oldValue.replace(/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
//
// replace oldValue with newValue
//
var str = originalString.replace(new RegExp(pattern, Flags), newValue);
return (str);
}
Czy już sprawdzone ten wpis? http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript – Patricia
Tak, wśród wielu innych. Jednak po ponownym jej przejrzeniu lepiej rozumiem, w jaki sposób używane jest wyrażenie regularne. Zmodyfikowałem powyższy kod, aby to odzwierciedlić. Dla mnie jest to bardziej zwięzła odpowiedź. Być może może to pomóc komuś innemu. –