javascript - 用于验证 AS 编号的正则表达式

标签 javascript regex jakarta-ee

我需要一个正则表达式来验证 Web 表单字段,该字段应包含 asdot 表示法中的 AS 编号,如 RFC 5396 中所述。 :

asdot

  refers to a syntax scheme of representing AS number values less
  than 65536 using asplain notation and representing AS number
  values equal to or greater than 65536 using asdot+ notation.
  Using asdot notation, an AS number of value 65526 would be
  represented as the string "65526" and an AS number of value 65546
  would be represented as the string "1.10".

我想使用 Javascript RegExp object和 Java EE javax.validation.constraints.Pattern用正则表达式。

最佳答案

这是一个 Javascript 正则表达式,可以满足您的要求:

/^([1-5]\d{4}|[1-9]\d{0,3}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(\.([1-5]\d{4}|[1-9]\d{0,3}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]|0))?$/   

假设:
不允许以 0. 开头的数字。
我认为点后带零的数字是允许的,例如65536 表示为 1.0。 点后的数字中不允许有前导零,例如1.00009 无效。
4字节AS号的最大值是4294967295,即65536*65535 + 65535,即asdot表示法的65535.65535

作为 Javascript RegExp 对象:

var asdot = new RegExp("^([1-5]\\d{4}|[1-9]\\d{0,3}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])(\\.([1-5]\\d{4}|[1-9]\\d{0,3}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5]|0))?$");

console.log( asdot.test('65535.65535') )   // true

作为 Java 模式:

Pattern asdot = Pattern.compile("^([1-5]\\d{4}|[1-9]\\d{0,3}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])(\\.([1-5]\\d{4}|[1-9]\\d{0,3}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5]|0))?$");

System.out.println( asdot.matcher("65535.65535").matches() );    // true

关于javascript - 用于验证 AS 编号的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14199418/

相关文章:

java - @Dependent CDI 注解是否被子类继承?

java - 异常: Could not parse query :containsOwn(text)

MysqlDataSource 的 Java EE ClassNotFoundException。我究竟做错了什么?

javascript - 函数内无法访问变量

PHP 和 JavaScript Ajax : how to get URL data when our method is "GET"

javascript - React/Redux - 如何在第一个函数完成获取 AJAX 后调用 componentDidMount 中的第二个函数

java - 正则表达式重复字符数

javascript - ajax POST 调用不同域上的 Web API

javascript - 从数组中查找所有可能的匹配字符串和子字符串

php - 使用正则表达式从 HTML 页面中提取 JSON (PHP)