简体   繁体   English

AngularJS过滤器中的regexp JavaScript

[英]regexp javascript in angularjs filter

This is my angularjs filter : 这是我的angularjs过滤器:

app.filter('cleanit', function() {
    return function (input) {
        input = input.replace(new RegExp('é'),'é');
        input = input.replace(new RegExp('É'),'É');
        input = input.replace(new RegExp('Ô'),'Ô');
        input = input.replace(new RegExp('''), '\'');
        return input;
    }
});

I use it for replace bad accents in feeds parsed with Google Feed API. 我用它来替换用Google Feed API解析的Feed中的不良口音。 It's works good but it's only works once per item, the replacement no longer takes place then, after the first success. 它的效果很好,但每个项目只能使用一次,第一次成功后,替换将不再发生。 what's wrong ? 怎么了 ?

As RevanProdigalKnight commented, you need to specify g modifier to globally replace matches: 正如RevanProdigalKnight所说,您需要指定g修饰符以全局替换匹配项:

input = input.replace(new RegExp('é', 'g'), 'é');

input = input.replace(/é/g, 'é');

BTW, here's a different way to solve your problem (instead of specifying charref, using replacement function.) 顺便说一句,这是解决您问题的另一种方法(而不是使用替换功能指定charref。)

input = input.replace(/&#x([a-f0-9]+);/ig, function($0, $1) {
    // The return value is used as a replacement string
    return String.fromCharCode(parseInt($1, 16));
});

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

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