简体   繁体   English

需要正则表达式以匹配网址

[英]Need Regex to match url

I have a list of urls starting with: 我有一个以下列网址开头的网址列表:

http://www.example.com
http://www.example.com/https://www.example.com
http://www.exampleTwo.com

I want to use regex to get all the urls starting from 我想使用正则表达式来获取所有从

http://www.example.com

This is what I have so far 这就是我到目前为止

var url = (' http://www.example.com');
var expression = /^http?:\/\/example\.com/\;
if (url != expression) { 
alert ("success");
}

Any one can shed a light on this please? 有人能对此有所启发吗?

I know I'm not actually answering your question, but I don't think it makes any sense to use regex here, when you can just use .indexOf , specifically: 我知道我实际上并没有在回答您的问题,但是当您只可以使用.indexOf ,我认为在这里使用正则表达式没有任何意义:

if(url.indexOf('http://www.example.com') == 0){
    alert('success');
}

If you need to use regex, feel free to disregard, 如果您需要使用正则表达式,请不要理会,

http://www.w3schools.com/jsref/jsref_regexp_test.asp http://www.w3schools.com/jsref/jsref_regexp_test.asp

var url = ('http://www.example.com');
var expression = new RegExp("^http:\/\/www\.example\.com.*");
if (expression.test(url)) { 
alert ("success");
}

Try this: 尝试这个:

var str = 'http://www.example.com';
str.match(/^http\:\/\/www\.example\.com.*$/);

Online Demo 在线演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM