Well, I need to split the the sentence into words and manipulate them. My problems is just the words that join with the special characters like me, or do?. I need that two words filtered without , or ? or any special characters. However, I need to join back the manipulated words with the special characters as input.
Anyone can help me? Snippet code so far the sample for my question.
var sentence = "Tell me, what should I do?";
var myApp = angular.module('myApp',[]);
myApp.controller('myCtrl',function($scope){
$scope.output = sentence.split(" ");
$scope.join_back = $scope.output.join(" ");
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl">
{{output}}
<hr/>
{{join_back}}
</div>
</div>
Try this regex to split your string.
sentence.split(/\s|(?=\W)/)
and to remove the space before punctuation use
$scope.output.join(" ").replace(/\s+(\W)/g, "$1");
var sentence = "Tell me, what should I do?";
var myApp = angular.module('myApp',[]);
myApp.controller('myCtrl',function($scope){
$scope.output = sentence.split(/\s|(?=\W)/);
$scope.join_back = $scope.output.join(" ").replace(/\s+(\W)/g, "$1");
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl">
{{output}}
<hr/>
{{join_back}}
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With