JSFiddle - React, Tailwind, and code Playground

JavaScript

var str = "foo 123456789.22 bar";

alert(str.replace(/\d(?=(?:\d{3})+(?!\d))/g, '$&,'));