正则表达式排除特定网址

时间:2017-07-18 05:57:36

标签: javascript regex

我尝试创建正则表达式以匹配来自我的域的网址,但会排除以下三个网址:

  • www.mydomain.com/
  • www.mydomain.com/contact
  • www.mydomain.com/about

如果具有查询参数,正则表达式还应排除这些URL。我正在努力解决这个问题,并且感谢任何帮助。

我尝试过类似于Rizwan发布的内容,但正如您所看到的,这个允许一些应该被排除的网址(例如test / www.mydomain.com /)

我也尝试过使用负面前瞻,但是没有做到这一点....它就像是

^(www.mydomain.com)?\/(?!.*(about|contact)).*$

2 个答案:

答案 0 :(得分:0)

正则表达式通常不是解析或检查网址的最佳方式。您应该使用URL对象来解析位置,但是如果条件是您所需的特定条件,则使用单个主机和路径名部分的正则表达可能仍然有用。例如:



const outputDiv = document.getElementById('output');
let output = '';
const urls = [
  'http://includeddomain.com/blarg',
  'http://includeddomain.com/blarg/anotherblarg',
  'http://includeddomain.com/about/otherstuff/',
  'http://www.includeddomain.com/blarg2',
  'http://subdomain2.includeddomain.com/blarg2/included',
  'http://includeddomain.com/contact',
  'http://includeddomain.com/about/',
  'http://anotherdomain.com/stuff',
].forEach(url => {
  const loc = new URL(url);
  const included = (
    loc.host.match(/(.*\.?)includeddomain.com/) && 
    ! loc.pathname.match(/(about|contact)(\/?)$/)
  );
  output += `<br>${loc.toString()} - ${included ? 'INCLUDED' : 'EXCLUDED'}`;
});

outputDiv.innerHTML = output;
&#13;
div {
  display: flex;
  align-items: center;
  justify-content: start;
}
&#13;
<div id="output"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用否定前瞻:

url = "www.mydomain.com/contact?name=John";
result = url.match(/^www\.mydomain\.com\/(?!about$|about\?|contact$|contact\?|\?|$).*/);
console.log(result);

www.mydomain.com/开头的字符串将匹配aboutcontact?,行尾($)和可选查询字符串。

或避免重复交替:

url = "www.mydomain.com/";
result = url.match(/^www\.mydomain\.com\/(?!(?=(?:about|contact)?(?=\?|$))).*/);
console.log(result);