javascript – Insert space after numbers

Question:

Previously, I wrote a validation to replace spaces in a string so that only a certain number of digits would be validated, now it is necessary that a space is automatically inserted after the digits (12b arrived, replaced with 12 b)

Validation.add('number-validation', '', function(v) {
var vReplace = v.replace(/\s+/g, '');
return Validation.get('IsEmpty').test(vReplace) || /^[0-9]{19,19}$/.test(vReplace);});

Answer:

var string = '12b 14d',
result = string.replace(/(\d+)/g, '$1 ');

console.log(result); // > 12 b 14 d

Read more: Sets and Ranges

Scroll to Top