如何使用angularjs分割字符串和表单数组

时间:2017-02-21 06:36:14

标签: javascript angularjs

我有两个字符串值,如果“,”或“。”需要拆分字符串。存在并且需要形成一个数组。

$scope.welcome = "Welcome to Adityavani facitlity services,   please select from the following";

$scope.final = "Thanks for calling. You have registered successfully";

我需要创建一个数组,如下所示:

$scope.welcome_array = ["Welcome to Adityavani facitlity services", "please select from the following"];

$scope.final_array = ["Thanks for calling", "You have registered successfully"];

5 个答案:

答案 0 :(得分:1)

$scope.welcome = "Welcome to Adityavani facitlity services,   please select from the following";
$scope.final = "Thanks for calling. You have registered successfully";

JS原生split([separator[, limit]]),正则表达式(/[,.]\s*/)作为分隔符:

$scope.welcome_array = $scope.welcome.split(/[,.]\s*/);
$scope.final_array = $scope.final.split(/[,.]\s*/);

答案 1 :(得分:0)

myslqdump

split 将用于拆分字符串并返回数组。

答案 2 :(得分:0)

split()与以下正则表达式一起使用:

/[,.]\s*/

$scope = {};

$scope.welcome = "Welcome to Adityavani facitlity services,   please select from the following";
$scope.final = "Thanks for calling. You have registered successfully";

$scope.welcome_array = $scope.welcome.split(/[,.]\s*/);
$scope.final_array = $scope.final.split(/[,.]\s*/);

console.log($scope.welcome_array);
console.log($scope.final_array);

答案 3 :(得分:0)

$scope = {};
$scope.welcome = "Welcome to Adityavani facitlity services,   please select from the following";
$scope.final = "Thanks for calling. You have registered successfully";
$scope.splitSrting=function(splitStr){
var reg=/[,.]\s*/;
return splitStr.split(reg);
}
console.log($scope.splitSrting($scope.welcome));
console.log($scope.splitSrting($scope.final));

答案 4 :(得分:-2)

$scope.welcome = $scope.welcome.split(",");
$scope.final = $scope.final.split(".");