我目前正在学习 Angular 我在转换某个长字符串时遇到问题。
所以,这是我的包含很多行的 div,由 ng-repeat 生成:
http://pastebin.com/raw/bJqqUvpY
是的,这很讨厌,我知道。我想要做的是从该字符串中删除我不需要的所有 ng 属性和其他内容,因为我将通过 ajax 将此数据传递给 PHP。
这是我尝试做的( Angular ):
$scope.updateHtml = function() {
var testVal = angular.element('#sendHtml').val();
testVal = testVal.replace(/ng-class="{'selected':selection.indexOf($index) != -1}"/g,"");
$scope.sendHtml.html = testVal;
};
但它不起作用。可能是因为短语里面有引号,还是真的?
例如,这适用于替换单个字母:
$scope.updateHtml = function() {
var testVal = angular.element('#sendHtml').val();
testVal = 'abcabcabc';
testVal = testVal.replace(/b/g,"");
$scope.sendHtml.html = testVal;
};
然后 $scope.sendHtml.html
就像它应该的那样等于 'acacac'
。
这可以用另一种 RegEx 来解决吗?
最佳答案
转义点 .
、圆括号 ()
和美元 $
符号。
testVal = testVal.replace(/ng-class="{'selected':selection\.indexOf\(\$index\) != -1}"/g,"");
在网站上,您可以通过打开菜单引用 - 转义字符来检查所有应该转义的字符。
关于javascript - 从 AngularJS 中的长字符串中删除特定短语(替换函数 + 正则表达式?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40749354/