Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture first digit after decimal

I have this line here:

\d(?!.*\d)

whereby it captures the last digit after a period. i.e: 6.3059

Will return "9", HOWEVER, what I really want it to do - is return 3.

like image 364
Mike Van Stan Avatar asked Oct 18 '22 20:10

Mike Van Stan


1 Answers

Your regex matches any digit that is not followed by a digit, that is why you get 9 as output.

You can use a capturing group:

\d\.(\d)

The value will be in Group 1. See demo.

JS code:

var re = /\d\.(\d)/; 
var str = '6.3059';
var m;
 
if ((m = re.exec(str)) !== null) {
    document.getElementById("r").innerHTML = m[1];
}
<div id="r"/>
like image 131
Wiktor Stribiżew Avatar answered Nov 15 '22 04:11

Wiktor Stribiżew