I have these information in the email from which I want to extract the phone number:
Numéro de réservation: 254207287398084
Téléphone (portable): 0782375426
When I try finding 0782375426
with Regex, it works perfectly well on regex101.com with that regular expression: /0[67][0-9]{8}\b
However, this part of my script returns null
:
var msg = messages[j].getBody();
var search = "0[67][0-9]{8}\b";
var regx = new RegExp("(.)" search "(.)");
var y = regx.exec(msg);
Logger.log(y);
Would you know what is wrong in my code?
CodePudding user response:
Replace "0[67][0-9]{8}\b"
by "0[67][0-9]{8}\\b"
and "(.)" search "(.)"
by search
PS. The regular expression used in your script is '(.)0[67][0-9]{8}\b(.)`, not the one that you have tested in regex101.com.
const msg =
`
Numéro de réservation: 254207287398084
Téléphone (portable): 0782375426
`;
var search = "0[67][0-9]{8}\\b";
var regx = new RegExp(search);
var y = regx.exec(msg);
console.log(y)
CodePudding user response:
First I would check the value of msg
console.log(msg);
If it contains some html then try
var msg = messages[j].getPlainBody();
But I would use a simplier regex. I used String.trim() because I get the blank space before the phone number also.
function test() {
try {
let msg = "Numéro de réservation: 254207287398084<br>Téléphone (portable): 0782375426<br>more";
let find = msg.match(/\s0[67]\d /);
console.log(find[0].trim())
}
catch(err) {
console.log(err);
}
}
1:58:28 PM Notice Execution started
1:58:30 PM Info 0782375426
1:58:29 PM Notice Execution completed