拆分正则表达式字符串

时间:2014-10-23 12:13:10

标签: javascript angularjs

我在一个字符串中定义了一个正则表达式为[a-zA-Z_0-9] {1,29},我只想从中提取[a-zA-Z_0-9]而我只是m使用以下代码来完成它。但是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

3 个答案:

答案 0 :(得分:0)

您要找的是substr

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

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

使用split的'标准'方法是这样的:

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

结果:

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

如上所述,你可能正在寻找substr()......