Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

正则作业 #77

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ module.exports = {
mobile: /^1\d{10}$/,
qq: /^[1-9]\d{4,10}$/,
number: /^[-+]?(?:\d+(?:\.\d*)|(?:\d*\.)?\d+)$/,
email: /^.$/,
url: /^.$/,
ipv4: /^.$/,
idcard: /^.$/,
};
email:
/^[A-Za-z0-9]+([A-Za-z0-9_-])*@[A-Za-z0-9]+([A-Za-z0-9_-])*(\.[A-Za-z0-9_-]+)*\.(com|cn|net|org|gov)$/,
url: /^(http(s)?\:\/\/)?([A-Za-z0-9]+[A-Za-z0-9_-])+(\.([A-Za-z0-9]+[A-Za-z0-9_-])+)+(\/[A-Za-z0-9_-]+)*(\/)?(\?[A-Za-z0-9]+([A-Za-z0-9_-])*=\w+(\&[A-Za-z0-9]+([A-Za-z0-9_-])*=\w)*)?$/,
ipv4: /^((\d|[1-9]\d|[1-2][1-4]\d|25[0-5])\.){3}(\d|[1-9]\d|[1-2][1-4]\d|25[0-5])$/,
idcard:
/^(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|71|81|82)(0[1-9]|[1-6]\d|70|90)(0[1-9]|[1-9]\d)(18|19|20)\d{2}(((01|03|05|07|08|10|12)(0[1-9]|[1-2]\d|3[0-1]))|((04|06|09|11)(0[1-9]|[1-2]\d|30))|(02(0[1-9]|[1-2]\d)))\d{3}[0-9Xx]$/,
}
74 changes: 51 additions & 23 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,57 @@ describe('正则表达式', () => {
});

it('匹配邮箱', () => {
[
'[email protected]',
].forEach((value) => {
assert.ok(RegExps.email.test(value));
});
[
'12345#qq.com',
].forEach((value) => {
assert.ok(!RegExps.email.test(value));
});
const validEmails = [
'[email protected]', // 数字邮箱
'[email protected]', // 字符邮箱
'[email protected]', // 组合邮箱
'[email protected]', // 支持多级域名
]
validEmails.forEach((value) => {
assert.ok(RegExps.email.test(value))
})
const invalidEmails = [
'leopen#qq.com.cn', // 不可缺少 @
'[email protected]', // 用户名不能以特殊字符开头
'@qq.com', // 用户名不能为空
'[email protected]', // 主机名不能为空
'leopen@xxxcom', // 域名不能缺少 .
'leopen@xxx.', // 不能缺少域名后缀
'[email protected]', // 不能使用不合法域名后缀
'[email protected]', // 域名不能存在连续的 .
]
invalidEmails.forEach((value) => {
assert.ok(!RegExps.email.test(value))
})
});

it('匹配 url', () => {
[
'https://www.qq.com',
].forEach((value) => {
assert.ok(RegExps.url.test(value));
});
[
'http//www.qq.com',
].forEach((value) => {
assert.ok(!RegExps.url.test(value));
});
const validUrl = [
'http://www.qq.com', // 支持 http
'https://www.qq.com', // 支持 https
'www.qq.com', // 支持无头
'qq.com', // 支持无头
'blog.sss.xd', // 支持自定义域名
'blog.sss.xd/sdm', // 支持路径
'blog.sss.xd/sdm/', // 支持尾部 /
'blog.sss.xd/sdm/?lo=x', // 支持query
'blog.sss.xd/sdm/?lo=x&df=3', // 支持多query
]
validUrl.forEach((value) => {
assert.ok(RegExps.url.test(value))
})
const invalidUrl = [
'http//www.qq.com', // 协议不能缺少 :
'cc://www.qq.com', // 不支持非 http https 协议
'.com', // 主机名不能为空
'xxxcom', // 域名不能缺少 .
'xxx.', // 不能缺少域名后缀
'xxx..com', // 域名不能存在连续的 .
'xxx.com//df', // 域名不能存在连续的 /
]
invalidUrl.forEach((value) => {
assert.ok(!RegExps.url.test(value))
})
});

it('匹配 IPv4', () => {
Expand All @@ -125,9 +153,9 @@ describe('正则表达式', () => {
});
[
'350301298906180060',
'350301298906310060',
'35030129890618006Y',
'3503012989061800666',
'350301199606310060',
'35030119960618006Y',
'3503011996061800666',
].forEach((value) => {
assert.ok(!RegExps.idcard.test(value));
});
Expand Down