Mikail Mikail - 4 months ago 8
Javascript Question

Javascript Regex returns null, node.js

I try to select a string based on regex, I use node.js ,here is my code :

var string = ' emaild@d.com Tel: +971000000000 0500000348';
var regExp = '\\(\\+971\\|00971\\|05\\)\\d\\{1,12\\}';
var find = string.match(regExp)[0];
console.log(find);


I need to select only numbers that starts with +971 , 00971, 05 , this returns null, (I had to escape twice coz it throws an error if I don't)
this regex works fine:
'\\+971\\d{1,12}';

Answer

I don't know node.js specific, so sorry if my answer would be stupid, but what about:

var string = ' emaild@d.com    Tel:   +971000000000 0500000348';
var regExp = /(\+971|00971|05)\d{1,12}/g;
var find = string.match(regExp);
return find;

enter image description here