其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

使用正则表达式验证ISBN号

GG网络技术分享 2025-03-18 16:15 1


问题描述:

I need a regex to verify ISBN number entered by user.

ISBN must be a string contains only:

[10 or 13 digits] and hyphens

I tried ^[\\d*\\-]{10}|[\\d*\\-]{13}$ but it doesn\'t work.

My regex only matches: 978-1-5661, 1-56619-90, 1257561035

It should returns the results below:

\"978-1-56619-909-4 2\" => false

\"978-1-56619-909-4\" => true

\"1-56619-909-3 \" => false

\"1-56619-909-3\" => true

\"isbn446877428ydh\" => false

\"55 65465 4513574\" => false

\"1257561035\" => true

\"1248752418865\" => true

I really appreciate any help.

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我需要一个正则表达式来验证用户输入的ISBN号。</ p>

ISBN 必须是一个只包含字符串的字符串:

[10或13位]和连字符</ code> </ p>

我试过 ^ [\\ d * \\ - ] {10 } | [\\ d * \\ - ] {13} $ </ code>但它不起作用。</ p>

我的正则表达式只匹配: 978-1-5661 </ 代码>, 1-56619-90 </ code>, 1257561035 </ code> </ p>

它应该返回以下结果:</ p>

 “978-1-56619-909-4 2”=&gt;  false 

“978-1-56619-909-4”=&gt; true

“1-56619-909-3”=&gt; false

“1-56619-909-3”=&gt; true

“isbn446877428ydh”=&gt; false

“55 65465 4513574”=&gt; false

“1257561035”=&gt; true

“1248752418865”=&gt; true

</ code> </ pre>

我真的很感激任何帮助。</ p>

</ div>

网友观点:

You can use this regex with a positive lookahead:

^(?=(?:\\D*\\d){10}(?:(?:\\D*\\d){3})?$)[\\d-]+$

RegEx Demo

(?=(?:\\D*\\d){10}(?:(?:\\D*\\d){3})?$) is a positive lookahead that ensures we have 10 or 13 digits in the input.

Javascript正则表达式验证账号、手机号、电话和邮箱

正则表达式是用于匹配字符串中字符组合的模式。在 JavaScript中,正则表达式也是对象。这些模式被用于 RegExp 的 exec 和 test 方法, 以及 String 的 match、replace、search 和 split 方法。本章介绍 JavaScript正则表达式。

1、验证帐号是否合法

验证规则:字母、数字、下划线组成,字母开头,4-16位。

2、手机号码

验证规则:11位数字,以1开头。

3、电话号码

验证规则:区号+号码,区号以0开头,3位或4位,号码由7位或8位数字组成,区号与号码之间可以无连接符,也可以“-”连接

4、邮箱

验证规则:姑且把邮箱地址分成“第一部分@第二部分”这样。第一部分:由字母、数字、下划线、短线“-”、点号“.”组成;第二部分:为一个域名,域名由字母、数字、短线“-”、域名后缀组成,而域名后缀一般为.xxx或.xxx.xx,一区的域名后缀一般为2-4位,如cn、com、net,现在域名有的也会大于4位

标签:

提交需求或反馈

Demand feedback