Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@chrisyip
Copy link
Contributor

修改了 URL 正则部分:

更快

screen shot 2015-02-11 at 2 18 36 pm

http://jsperf.com/validator-js-catched-regexp

对没有顶级域名的 URL 应只支持 localhost

var re = new RegExp('^(?:localhost|(?:[a-z0-9]+(?:[-\\w]*[a-z0-9])?(?:\\.[a-z0-9][-\\w]*[a-z0-9])*)*\\.[a-z]{2,})$', 'i')

console.log('should true:')
console.log(re.test('localhost'));
console.log(re.test('domian.hello'));
console.log(re.test('domi-an.hello'));
console.log(re.test('dom.ian.hello'));
console.log(re.test('dom.i-an.hello'));
console.log(re.test('1.dom.i-an.hello'));
console.log(re.test('12306.cn'));
console.log(re.test('12306.com.cn'));

console.log('should false:')
console.log(re.test('dom.-ian.hello'));
console.log(re.test('dom.ian-.hello'));
console.log(re.test('dom.i+an.hello'));
console.log(re.test('dom.ian..hello'));
console.log(re.test('hello'));

旧方法:

var re = new RegExp('^(?:[a-z0-9]+(?:\\-[\\w]+)*\\.)*[a-z]{2,}$', 'i')

console.log('should false:')
console.log(re.test('hello')); // true

- Faster
- Should only support `localhost` for URL without top-level domain
sofish added a commit that referenced this pull request Feb 11, 2015
@sofish sofish merged commit bb6207d into sofish:master Feb 11, 2015
@sofish
Copy link
Owner

sofish commented Feb 11, 2015

牛逼 👍

@chrisyip chrisyip deleted the url-validator branch February 11, 2015 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants