拆分正则表达式字符串

Split a regular expression string

本文关键字:字符串 正则表达式 拆分      更新时间:2023-09-26

我在一个字符串中有一个正则表达式定义为[a-zA-Z_0-9]{1,29},我只想从中提取[a-zA-Z_0-9],我使用下面的代码来完成。但console.log返回我["[a-zA-Z _","-9]{1.29}"]作为输出。我哪里错了?

app.controller('MainCtrl', function($scope) {
  $scope.val = "";
  $scope.regEx = "[a-zA-Z_0-9]{1,29}";
  $scope.pattern = $scope.regEx.split(0,12);
  console.log($scope.pattern) //it should return [a-zA-Z_0-9]
}); 

Plnkr:http://plnkr.co/edit/PFLZXs6er2lG3ieG4mi8?p=preview

使用拆分的"标准"方法如下:

$scope.val = "abc;def;ghi";
$scope.regEx = /'s*;'s*/;
$scope.list = $scope.val.split($scope.regEx);
console.info($scope.list);

结果:

["abc", "def", "ghi"]

如前所述,您可能正在查找substr()。。。

您要查找的是substr

$scope.pattern = $scope.regEx.substr(0,12);

试试这个:

$scope.pattern = $scope.regEx.substr(0,12);