Jam*_*mms 35 javascript typescript
来自其他编程语言,String.replace()通常替换所有匹配的字符串.但是,javascript/typescript不是这种情况.我在网上找到了许多使用正则表达式的javascript解决方案.由于特殊字符,我立即遇到了这个解决方案的问题.我怀疑有一种方法可以用正则表达式来解决这个问题,但我不是一个正则表达式专家.正如我之前所做的那样,我创造了自己的方法.
也许有一些方法可以通过使用自定义StringBuilder()类来提高性能.我欢迎任何想法.
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;
}
Run Code Online (Sandbox Code Playgroud)
为了向string该类添加对此方法的支持,我添加了以下代码:
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);}
Run Code Online (Sandbox Code Playgroud)
....在重新查看其他帖子后,我修改了代码以使用正则表达式.执行性能测试会很有趣.
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);
}
Run Code Online (Sandbox Code Playgroud)
bas*_*rat 54
在typescript中,String.Replace只替换第一次出现的匹配字符串.需要String.replaceAll()方法
这里没有什么特别的TypeScript(毕竟TypeScript只是带有类型注释的JavaScript).string.replace如果给定字符串,JavaScript 仅替换第一个实例.获得全部替换的唯一方法是使用regexwith /g修饰符.
或者我只是这样做:
somestring.split('oldString').join('newString');
Run Code Online (Sandbox Code Playgroud)
就我而言,我在TypeScript中确实做到了这一点。
this.mystr= this.mystr.replace(new RegExp('class="dec-table"', 'g'), 'class="copydec-table"');
Run Code Online (Sandbox Code Playgroud)