asked    Violet     2018-10-22       javascript       45 view        3 Answers

[SOLVED] How do I get a number to render in my UI with a space after every 4 characters?

I have a 16 digit property that comes back for example(1234123412341234). I would like it to render on my UI with a space every 4 number characters for example(1234 1234 1234 1234). I'm using Angular JS so I'm looking for a way to accomplish this. I'm pretty sure it's either a custom Angular filter or regex related, but my knowledge of either and how to implement it is limited.

  3 Answers  

        answered    Penny     2018-10-22      

AngularJS filter is easy to setup. Just accept one parameter and apply any JS formatting on it. Here is a working example:

var app = angular.module('myApp', []);
app.filter('myFilter', function() {
  return function(x) {
    return x.toString().replace(/\d{4}(?=.)/g, '$& '); // your format filter here
  };
});
app.controller('demoCtrl', function($scope) {
  $scope.test = 1234123412341234;
});
<!DOCTYPE html>
<html>
<script src=""></script>
<body>
  <div ng-app="myApp" ng-controller="demoCtrl">
    {{test | myFilter}}
  </div>
</body>
</html>


        answered    Rosalind     2018-10-22      

You can use a regex with lookahead.

The Positive Lookahead looks for the pattern after the equal sign, but does not include it in the match.

x(?=y)

Matches 'x' only if 'x' is followed by 'y'. This is called a lookahead.

For example, /Jack(?=Sprat)/ matches 'Jack' only if it is followed by 'Sprat'. /Jack(?=Sprat|Frost)/ matches 'Jack' only if it is followed by 'Sprat' or 'Frost'. However, neither 'Sprat' nor 'Frost' is part of the match results.

function format(s) {
    return s.toString().replace(/\d{4}(?=.)/g, '$& ');
}

console.log(format(1234123412341234));


        answered    Quentin     2018-10-22      

You can use match() and then join() with a whitespace.

var str = '1234123412341234';
var res = str.match(/.{1,4}/g).join(' ');
console.log(res);




Your Answer





 2018-10-22         Len

EXC_BAD_ACCESS when adding object to array

I am new programming and C++. I am trying to create a Roster of Person objects using an array and then printing the attributes of the People in the Roster.When I attempt to add a Person to the personArray, I am getting an Exception = EXC_BAD_ACCESS (code=1, address=0x0).I think this has to do with the scope of my personArray but I can't seem to figure it out.Here is the code I am using:#include <iostream>#include <sstream>#include <vector>using namespace std;class Person {public: Person(string name, int age); string getName() { return name; ...
 c++                     2 answers                     25 view
 2018-10-22         Hayden

Check and add for property in object of arrays

I have an object/output in a very specific format detail below:result = AB: [ 0:{Name: "Tom", team:"Spirit", position: "Defender", score: 22 } 1:{Name: "Paul", team:"Vikings", position: "center" } 2:{Name: "Jim", team:"United", position: "Wing", } 3:{Name: "Greg", team:"Crusaders", position: "Fullback", score: 54} 4:{Name: "Tim", team:"Vikings", position: "Fullback", score: 77 } ]CD: [0:{...},1:{...},2:{...},3:{...},4:{...}]EF: [0:{...},1:{...},2:{...},3:{...},4:{...}]GH: [0:{...},1:{...},2:{...},3:{...},4:{...}]The result has nested arrays. On some outputs t...
 javascript                     1 answers                     25 view
 2018-10-22         Dolores

I'm trying to figure out how to add the values in an object using a function

Here's the code. I left the function block blank because I'm stumpedfunction getSummedAge(people) {}const examplePeopleArray = [ { name: 'John', age: 10 }, { name: 'Jack', age: 20 }, { name: 'Jane', age: 25 }];console.log(getSummedAge(examplePeopleArray)); Checkout the Array.reduce method! It's purpose is to basically reduce an array into a single value, which I believe is what you're trying to do here: Array.reduce() MDNHere's a little example of how you might use it in this context function sumAllApplesIn(baskets){ return baskets.reduce((accumulator, curre...
 javascript                     2 answers                     26 view