Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS,如何在正则表达式中使用捕获组_Angularjs_Regex - Fatal编程技术网

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>'
    }));
};