JavaScript matching domains and partial -


var url = 'http://sub.domain2.net/contact/';  if (['https://sub.domain1.com/', 'http://sub.domain2.net/'].indexof(url) > -1) {     console.log('match'); } else {     console.log('no match'); } 

in above checks whether url matches of 2 in array. want partial match domain.

the url http://sub.domain2.net/contact/ should match tld in array.

use array.prototype.some:

var url = 'http://sub.domain2.net/contact/';  var didmatch = ['https://sub.domain1.com/', 'http://sub.domain2.net/'].some(function(u) {     return url.indexof(u) !== -1; });  if (didmatch) {     console.log('match'); } else {     console.log('no match'); } 

replace url.indexof(u) url.startswith(u) if want check against starting of urls in array.

if want support <ie9, use polyfill.


Comments