0

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?

3 Answers3

3

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:

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

If you need to use regex, feel free to disregard,

Jake Haller-Roby
  • 5,998
  • 1
  • 16
  • 29
1

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");
}
An Overflowed Stack
  • 304
  • 1
  • 5
  • 19
0

Try this:

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

Online Demo

Shafizadeh
  • 9,086
  • 10
  • 43
  • 80