简体   繁体   中英

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}';

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;

在此处输入图片说明

(\+971|00971|05)(\d{1,12})

Get group 2 and you should get what you want. I am not sure why you need to escape also. It is working for me without escaping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM