Javascript 替换仅替换匹配字符串的第一个匹配项。如何替换*所有*事件?
来自其他编程语言的Javascript 替换仅替换匹配字符串的第一个匹配项。如何替换*所有*事件?,javascript,regex,typescript,Javascript,Regex,Typescript,来自其他编程语言的String.replace()通常替换所有匹配字符串。然而,javascript/typescript并非如此。我在web上发现了许多利用正则表达式的javascript解决方案。由于特殊的字符,我立即对此解决方案产生了问题。我怀疑有一种方法可以用正则表达式纠正这个问题,但我不是正则表达式专家。正如许多人在我之前所做的那样,我创建了自己的方法 也许可以通过使用自定义的StringBuilder()类来提高性能。我欢迎任何想法 public static Replace = f
String.replace()
通常替换所有匹配字符串。然而,javascript/typescript并非如此。我在web上发现了许多利用正则表达式的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;
}
为了向字符串添加对该方法的支持,I类添加了以下代码:
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);}
…在重新查看其他帖子后,我修改了代码以使用正则表达式。执行性能测试会很有趣
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);
}
在typescript中,String.Replace仅替换匹配字符串的第一个匹配项。需要String.replaceAll()方法
这里的TypeScript没有什么特别之处()。JavaScriptstring.replace
仅在给定字符串时替换第一个实例。获得全部替换的唯一方法是使用带有/g
修饰符的regex
或者,我只是:
somestring.split('oldString').join('newString');
在我的例子中,我在打字脚本中就这样做了
this.mystr= this.mystr.replace(new RegExp('class="dec-table"', 'g'), 'class="copydec-table"');
在我的例子中,我使用的是节点12+。并且节点没有.replaceAll()
(请检查)
但是我的解决方案是使用带有.replace()
的正则表达式(/g)。该键正在使用/g
const s=`[人员、合同、设施]`;
//我的情况是删除`[`和`]`
//所以关键是使用`/g`
常量res=s.replace(/\[/g')。replace(/\]/g');
你已经看过这篇文章了吗?是的,还有很多。然而,在重温它之后,我对正则表达式是如何使用的有了更好的理解。我修改了上面的代码以反映它。对我来说,这是一个更简洁的答案。也许它可以帮助其他人。是的,String.split().join()方法既短又甜。但是,不可能执行不区分大小写的搜索/替换。我第一次使用带有/g修饰符的正则表达式时,它在代码执行期间为正则表达式生成了一个无效的模式。我需要在模式中加入转义序列<代码>变量模式=旧值。替换(/[-\[\]\/\{\\\\\(\)\*\+\?\。\\\\^\$\\\\;]/g,“\$&”)这就是为什么我喜欢在我的示例中包含整个函数。这个过程很简单,很短,我喜欢这种分割连接的东西!作为参考,这里的TypeScript replace调用使用regex:myString.replace(/oldString/g,'newString)代码>正则表达式在必须替换特殊字符(“[”,“]]”)时失败@johanapeling它不需要失败。您总是可以转义特殊字符,如(“\[”,“\]”)