繁体   English   中英

使用正则表达式替换/清除子字符串的性能问题

[英]Performance issue using regex to replace/clear substring

我有一个包含以下内容的字符串:

<a@{style}@{class}@{data} id="@{attr:id}">@{child:content} @{child:whatever}</a>

除了以@{child: .开头的子字符串之外,这里所有要做的事情都是清晰的@{xxx} @{child: .

我使用str.match()获取数组中的所有子字符串"@{*}"来搜索并保留所有@{child: substrings:

var matches = str.match(new RegExp("@\{(.*?)\}",'g'));
if (matches && matches.length){
    for(var i=0; i<matches.length; i++){
        if (matches[i].search("@{child:") == -1) str = str.replace(matches[i],'');  
    }
}

我让它运行正常,但是当字符串变大时它太慢了(〜2秒/ +1000个这样的节点在顶部)

是否有其他替代方法,也许使用规则(如果存在)来转义@{child:直接在正则表达式中并提高性能?

如果我正确地理解了您的问题,则您不想删除@{child:...}子字符串,但其他所有格式为@{...}子字符串都应该删除。 在这种情况下,您可以更改正则表达式以在执行替换时检查child:是否不匹配:

var str = '<a@{style}@{class}@{data} id="@{attr:id}">@{child:content} @{child:whatever}</a>';
str = str.replace(/@\{((?!child:)[\s\S])+?\}/g, '');

这看起来非常快。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM