javascript - Cannot get all possible overlapping regular expression matches -
i have string
started: 11.11.2014 11:19:28.376<br/>ended: 1.1.4<br/>1:9:8.378<br/>request took: 0:0:0.2
i need add zeros in case encounter 1:1:8 should 01:01:08 same goes date. tried using
/((:|\.|\s)[0-9](:|\.))/g
but did not give possible overlapping matches. how fix it?
var str = "started: 11.11.2014 11:19:28.376<br/>ended: 11.11.2014<br/>11:19:28.378<br/>request took: 0:0:0.2"; var re = /((:|\.|\s)[0-9](:|\.))/g while ((match = re.exec(str)) != null) { //alert("match found @ " + match.index); str = [str.slice(0,match.index), '0', str.slice(match.index+1,str.length)]; } alert(str);
this want:
str.replace(/\b\d\b/g, "0$&")
it searches lone digits \d
, , pad 0
in front.
the first word boundary \b
checks there no [a-za-z0-9_]
in front, , second checks there no [a-za-z0-9_]
behind digit.
$&
in replacement string refers whole match.
if want pad 0
long character before , after not digits:
str.replace(/(^|\d)(\d)(?!\d)/g, "$10$2")
Comments
Post a Comment