AngularJS,如何在正则表达式中使用捕获组
在我的项目中,用户可以输入字符串,然后在视图中将其转换为按钮 以下是我希望发生的一个例子: 用户输入:AngularJS,如何在正则表达式中使用捕获组,angularjs,regex,Angularjs,Regex,在我的项目中,用户可以输入字符串,然后在视图中将其转换为按钮 以下是我希望发生的一个例子: 用户输入:argumentation-link\u至(80) 在视图中,它应该如下所示: <button ng-click="goToArgumentation(80)" class="btn btn-xs btn-info"> a link </button> 我需要在argumentation-link\u to(80)中获取编号80,以在goToArgumentat
argumentation-link\u至(80)
在视图中,它应该如下所示:
<button ng-click="goToArgumentation(80)"
class="btn btn-xs btn-info"> a link
</button>
我需要在argumentation-link\u to(80)
中获取编号80
,以在goToArgumentation()中填写参数
谢天谢地,Regex为我们提供了在Regex中使用捕获组检索数据的选项。但是,当我尝试使用它们时,我得到一个错误,即正则表达式无效,我试过:
/argumentation-link_to\((?<id>\d+)\)/i
/argumentation-link_to\((?'id'\d+)\)/i
在angular js中使用捕获组的正确方法是什么?我将如何引用它们?要使用您使用的捕获组()和RegExp的exec()函数:
$scope.buttonmaker = function(haystack) {
needle = /argumentation-link_to\((\d+)\)/i; // <-- Regex
haystack = $sanitize(haystack);
return $sce.trustAsHtml(haystack.replace(new RegExp(needle, "gi"), function(match) {
console.log(match);
return '<button ng-click="goToArgumentation(' + needle.exec(match)[1] + ')" class="btn btn-xs btn-info"> a link </button>'
}));
};
$scope.buttonmaker=函数(haystack){
指针=/argumentation-link_to \(\d+)/i;//使用/argumentation-link_to \(\d+)/i
并通过匹配[1]
@WiktorStribiżew访问如果我这样做,它将返回:一个链接
/argumentation-link_to\((?<id>\d+)\)/i
/argumentation-link_to\((?'id'\d+)\)/i
/argumentation-link_to\((?:\d+)\)/i
$scope.buttonmaker = function(haystack) {
needle = /argumentation-link_to\((\d+)\)/i; // <-- Regex
haystack = $sanitize(haystack);
return $sce.trustAsHtml(haystack.replace(new RegExp(needle, "gi"), function(match) {
console.log(match);
return '<button ng-click="goToArgumentation(' + needle.exec(match)[1] + ')" class="btn btn-xs btn-info"> a link </button>'
}));
};