Regex in javascript for partial match in URL

Asked

Viewed 524 times

3

I need to write a javascript regex that contains a specific piece of URL, but I’m not able to, mainly because it’s a url with "/".

Example: Any "like" or partial match to "www.url.com/foo"

www.url.com/foo/bar/send/123   - true
www.url.com/foo/doe/get/123    - true
www.url.co/foo/doe/get/123     - false
http://www.url.com/foo/get/123 - true
http://www.url.co/foo/doe/123  - false

Any idea how to make a "generic" regex for any other urls as well?

  • The url.com it’s fixed whatever comes next can be anything?

  • Have you tried declaring the regular expression Regexp? var re1 = new Regexp("abc");

3 answers

3


You can use the .test of Regexp:

/\w+\.com(\/[\/\w]*)?$/.test(url);

Remembering to escape from "/".

var url = "www.url.com/foo/bar/send/123"; 
var res = /\w+\.com(\/[\/\w]*)?$/.test(url);

console.log(url, ' -> ', res);

var url = "www.url.co/foo/bar/send/123"; 
var res = /\w+\.com(\/[\/\w]*)?$/.test(url);

console.log(url, ' -> ', res);

  • I believe it worked, thank you! I was stuck in the question of using "/" to signal bars.

  • @Jhonathan with this answer if I have the following URL: qu@alq3ercoisaAqu!->.url.com/foo/bar/send/123 the result will still be TRUE

  • 1

    This code limits you to only one specific domain and not to any domain.

  • @Thiagooliveira truth, well noted.

1

You can use this regex: /(http:\/\/)?www\.\w+\.com.+/g as follows:

var regExp = /(http:\/\/)?www\.\w+\.com.+/g;
var url = 'url-aqui';
var resultado = regExp.test(url); //retorna true ou false

So you take the URL o http:// which may or may not exist, the www, any text you are after and end the domain with .com.

  • And if it’s https, or ftp or file?

  • Then you can modify the expression as follows: /(http:\/\/)?(https:\/\/)?(ftp:\/\/)?(file:\/\/)?(www\.)?\w+\.com.+/g. So you can include any protocol you want to check, and also consider Urls that do not contain the www.

  • With this answer if you have anything before http, it will also give TRUE, would have to add the ^ after the / At the beginning of Pattern to stay straight.

  • You can get that look on your face: /^((http|https|ftp|file):\/\/)?(www\.)?\w+\.com.+$/g.

0

The best way to work with url is to put it in a string and use Regexp, as it automatically escapes the bars, so you can write your test url normally.

var url = "www.url.com/foo/bar/";
r = new RegExp(url);
console.log(r);

From what I understand what you want to check is if the domain is correct for this you can do so.

var dominio = 'www.url.com';
var r = new RegExp(dominio);

console.log(r)

var testes = [
  'www.url.com/foo/bar/send/123',
  'www.url.com/foo/doe/get/123',
  'www.url.co/foo/doe/get/123',
  'http://www.url.com/foo/get/123',
  'http://www.url.co/foo/doe/123',
]

for(var i in testes){
  var url = testes[i];
  console.log(url, url.match(r)!=null);
}

And so to check the url with foo just change to var dominio = 'www.url.com/foo'

  • Beware of the point in regex, this regex can marry wwwwurl.com with no problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.