6

Im having some strings like these

aa11b2s
abc1sff3
a1b1sdd2

etc.... i need to change these strings to these

aa 11 b 2 s
abc 1 sff 3
a 1 b 1 sdd 2

Simply saying..i need to add a space between each(number/alphabetic s) blocks

coolguy
  • 7,866
  • 9
  • 45
  • 71

3 Answers3

3
var str = 'aa11b2s'.replace(/([a-z]+|\d+)(?!$)/gi, '$1 ');
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
2
var str = 'aa11b2s';
console.log(str.replace(/([\d.]+)/g, ' $1 ').replace(/^ +| +$/g, ''));
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • If i use aa11b2.5s its getting aa 11 b 2 . 5 s i dont want the space between the decimals..hw can i omit that – coolguy May 22 '12 at 04:56
2
result = subject.replace(/[a-z](?=[0-9])|[0-9](?=[a-z])/ig, "$& ");

This matches a letter that's followed by a digit, or a digit that's followed by a letter, without consuming the second character. Then it replaces the first character with the same character followed by a space.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156