将preg_match模式转换为javascript模式

时间:2011-05-30 10:58:47

标签: javascript regex preg-match

任何人都可以帮助将这个php正则表达式转换为js兼容的吗?

$delimiter = "-";
$phone = "some number from the client";
var_dump(
    preg_match($phone,"|^([0-9]{1,3}".$delimiter."[0-9]{1,4}".$delimiter."[0-9]{4,12}(#[0-9]{1,4})?)$|")
);

由于

1 个答案:

答案 0 :(得分:1)

var delimiter = "-",
    phone = "8-989-895",
    re = new RegExp("^[0-9]{1,3}"+delimiter+"[0-9]{1,4}"+delimiter+"[0-9]{4,12}(?:[0-9]{1,4})?$");
console.log(re.test(phone));