Home > database >  How to extract numbers from email
How to extract numbers from email

Time:06-05

I am trying to only extract a confirmation number from string "Your confirmation number is 35657764. Please keep this number for future reference."

I am using:

var emailKeywords = {
  body: "Your confirmation number is"}
  
var regExp;
  regExp = new RegExp("(?<="   emailKeywords.body   ").*");

I am getting back:

"* 35657764 *. Please"

What I need:

35657764 

CodePudding user response:

This works for me.

String.match(/d /g)[0]

CodePudding user response:

If the message does not contain any other numbers then use \d . Otherwise, you can get the number exactly directed next to "Your confirmation number is " using

new RegExp("(?<="   emailKeywords.body   ")(.*?)(?=\\.)")

Code example

var message = "Your confirmation number is 35657764. Please keep this number for future reference.";
var emailKeywords = {body: "Your confirmation number is "};

var regexp = new RegExp("(?<="   emailKeywords.body   ")(.*?)(?=\\.)");
console.log(message.match(regexp)[0]);  // 35657764
  • Related