正在删除'<以及那些>'来自javascript中的字符串

Removing '<and text within those>' from a string in javascript

本文关键字:javascript 来自 字符串 gt 删除 lt 那些      更新时间:2023-09-26

我从RESTapi调用中收到一个字符串数组,这些字符串中有一些html标记,例如:

Bard's opponents can also travel through his Magical Journey doorways. You can follow him, if you think it's safe.</li>
You can crush Bard's healing shrines just by walking over them. Don't let his allies take them without a fight.
Bard's ultimate, Tempered Fate, affects allies, enemies, monsters, and turrets alike. Sometimes it can be to your advantage to jump into it!</li>

每一行都是一个字符串,其中两行的末尾都有一个</li>标记。

我尝试编写一个函数来接收这样的数组,并返回一个已更正的数组。问题是,当我使用它时,我网站上的控制台会显示数组中字符串的一些奇怪错误,我已经意识到我的函数是原因。

这就是功能:

modal.removeBracketsFromArray = function (array) {
    if (array == undefined)
        return array;
    function removeBracketsText(text) {
        return text.replace(/<[^>]*>/g, '')
    };
    var newArray = [];
    for (var i = 0; i < array.length; i++) {
        newArray.push(removeBracketsText(array[i]));
    }
    return newArray;
 };

它似乎完成了这项工作,但在ng-repeat属性中使用它时,有些原因会出错。

这是一个使用示例:

<champion-ally-enemy-tips allytips="modalCtrl.removeBracketsFromArray(modalCtrl.champ.allytips)"
                          enemytips="modalCtrl.removeBracketsFromArray(modalCtrl.champ.enemytips)">
</champion-ally-enemy-tips>

然后移动到:

<ul>
    <li ng-repeat="enemytip in enemytips"><h6>{{enemytip}}</h6></li>
</ul>

当我删除方法调用时(就像这样),它不会显示错误,但标签仍然存在:

<champion-ally-enemy-tips allytips="modalCtrl.champ.allytips"
                          enemytips="modalCtrl.champ.enemytips">
</champion-ally-enemy-tips>

我的函数是否在不知不觉中做了一些奇怪的事情?感谢您帮助

这是我收到的错误的粘贴框:LINK

与其扰乱数组,不如创建一个自定义过滤器来去除显示的HTML?

.filter('removeHTML', function() {
    return function(input) {
        return input.replace(/<[^>]*>/g, '');
    }
})

然后将ng-repeat内的显示更改为:

<h6>{{enemytip | removeHTML}}</h6>