RegEx for extracting the last combination of numbers from a paragraph
E.g.:
“My ID is 112243 and my phone number is 0987654321. Thanks you.”
So I want to extract the phone number here which is the last combo of digits.
RegEx for extracting the last combination of numbers from a paragraph
E.g.:
“My ID is 112243 and my phone number is 0987654321. Thanks you.”
So I want to extract the phone number here which is the last combo of digits.
You can use this regex,
\d+(?=\D*$)
Here, \d+ selects one or more number and this positive lookahead (?=\D*$) ensures that if at all anything is present further ahead before end of string then it is not a digit (by using \D* before $).
JS Code demo,
const s = 'My ID is 112243 and my phone number is 0987654321. Thanks you.'
console.log(s.match(/\d+(?=\D*$)/)) 
    
    var string = "My ID is 112243 and my phone number is 0987654321. Thanks you";
var numbers = string.match(/\d+/g).map(Number);
console.log(numbers);It will give you all combo of numbers in an array. You can choose which one you want.
 
    
    You can also use a simple regex like ^.*\b(\d+)
var str = 'My ID is 112243 and my phone number is 0987654321. Thanks you';
var num = str.match(/^.*\b(\d+)/)[1];
console.log(num);.* will consume anything before the last \b word boundary followed by \d+ digits.\d+ will capture the stuff inside to [1][\s\S]* instead of .* to skip over newlines.