├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.10 4 | - 0.11 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2015 John Otander 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Domain Regex 2 | 3 | [![Build Status](https://travis-ci.org/johnotander/domain-regex.svg?branch=master)](https://travis-ci.org/johnotander/domain-regex) 4 | 5 | A regular expression for matching valid domain names. This regex seeks to support all 6 | domains with TLDs listed on 7 | [publicsuffix.org](https://publicsuffix.org/list/effective_tld_names.dat) including 8 | [IDNs](http://en.wikipedia.org/wiki/Internationalized_domain_name). 9 | 10 | In the near future this will be likely moved to . 11 | 12 | ## Installation 13 | 14 | ``` 15 | npm i --save domain-regex 16 | ``` 17 | 18 | ## Usage 19 | 20 | ```javascript 21 | var domain = require('domain-regex'); 22 | 23 | domain().test('example.aerodrome.aero') // => true 24 | domain().test('a.sub.domain.org') // => true 25 | domain().test('invalid_domain') // => false 26 | ``` 27 | 28 | ### IDN Support 29 | 30 | This regex requires any Unicode character to be converted to its ASCII equivalent. This can be done 31 | with a library like [punycode.js](https://github.com/bestiejs/punycode.js). 32 | 33 | ```javascript 34 | var domainRegex = require('domain-regex'); 35 | var punyCode = require('punycode'); 36 | 37 | domainRegex().test(punycode.toASCII('岡山.jp')) // => true 38 | ``` 39 | 40 | ## Acknowledgements 41 | 42 | * Regex adapted from . 43 | 44 | ## License 45 | 46 | MIT 47 | 48 | ## Contributing 49 | 50 | 1. Fork it 51 | 2. Create your feature branch (`git checkout -b my-new-feature`) 52 | 3. Commit your changes (`git commit -am 'Add some feature'`) 53 | 4. Push to the branch (`git push origin my-new-feature`) 54 | 5. Create new Pull Request 55 | 56 | Crafted with <3 by [John Otander](http://johnotander.com). 57 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function() { 4 | return /\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b/; 5 | }; 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "domain-regex", 3 | "version": "0.0.1", 4 | "description": "A regular expression for valid domains.", 5 | "main": "index.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "mocha test" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/johnotander/domain-regex.git" 15 | }, 16 | "keywords": [ 17 | "domain", 18 | "regex", 19 | "regexp", 20 | "regular-expression" 21 | ], 22 | "author": "John Otander (http://johnotander.com/)", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/johnotander/domain-regex/issues" 26 | }, 27 | "homepage": "https://github.com/johnotander/domain-regex", 28 | "devDependencies": { 29 | "mocha": "^2.0.1", 30 | "punycode": "^1.3.2" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var punycode = require('punycode'); 3 | var domain = require('..'); 4 | 5 | describe('domain-regex', function() { 6 | 7 | // https://publicsuffix.org/list/effective_tld_names.dat 8 | it('should handle a wide array of domains', function() { 9 | var validDomains = [ 10 | 'some-example.construction', 11 | 'example.co.uk', 12 | 'example.aerodrome.aero', 13 | 'g.co' 14 | ]; 15 | 16 | validDomains.forEach(function(validDomain) { 17 | assert.ok(domain().test(validDomain)); 18 | }); 19 | }); 20 | 21 | it('should handle IDNs', function() { 22 | var validDomains = [ 23 | punycode.toASCII('something.組织.hk'), 24 | punycode.toASCII('組織.tw'), 25 | 'example.2000.hu', 26 | punycode.toASCII('岡山.jp') 27 | ]; 28 | 29 | validDomains.forEach(function(validDomain) { 30 | assert.ok(domain().test(validDomain)); 31 | }); 32 | }); 33 | 34 | it('should not find a domain when it does not exist', function() { 35 | var invalidDomains = [ 36 | 'notvalid.com.', 37 | '.notvalid.com', 38 | '-notvalid.com', 39 | 'notvalid.com-', 40 | 'this.istoolongofatldrighthere.com', 41 | ]; 42 | 43 | invalidDomains.forEach(function(invalidDomain) { 44 | assert.ok(domain().test(invalidDomain)); 45 | }); 46 | }); 47 | }); 48 | --------------------------------------------------------------------------------