Capturing the first digit after decimal

I have this line here:

\d(?!.*\d)

whereby he fixes the last digit after the period. i.e.: 6.3059

Will return "9", HOWEVER, what I really want is a return of 3.

+4
source share
1 answer

Your regular expression matches any digit that is not followed by a digit, so you get 9as output.

You can use capture 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"/>
Run codeHide result
+3
source

Source: https://habr.com/ru/post/1620130/


All Articles