search

Home  >  Q&A  >  body text

JavaScript regular expressions for URL detection and splitting

I have the following URL:

https://comanage.example.edu/sp
https://wiki.cs.example.org/sp
https://intranet.math.example.edu/sp
https://myapp.example.com/sp

For these URLs, I need to define a function to detect whether they are URLs and replace the https:// and sp paths from them. Basically, I just need the hostname. For example, as shown below:

https://comanage.example.edu/sp      ->comanage.example.edu
https://wiki.cs.example.org/sp       ->wiki.cs.example.org
https://intranet.math.example.edu/sp ->intranet.math.example.edu
https://myapp.example.com/sp         ->myapp.example.com

For non-URLs, this function should detect and not replace them. As follows:

nonurl.example.com -> ***no replacement***

Can anyone provide me with a solution to the above problem? I don't have much knowledge about using regular expressions.

P粉043432210P粉043432210439 days ago637

reply all(1)I'll reply

  • P粉680087550

    P粉6800875502023-09-22 12:48:19

    Mode ^https?:\/\/ should be easy to use here. We can use it to replace any string starting with http:// and https:// with the empty string

    In a pattern, the ^ symbol represents the beginning of a string. This means that if http:// appears in the middle of the string, it will not match since it must be

    at the beginning

    ? Marks the previous character as optional. In the pattern, s is optional in order to find http and https

    \/ is required because slashes must be escaped

    const urls = [
      'https://comanage.example.edu/sp',
      'https://wiki.cs.example.org/sp',
      'https://intranet.math.example.edu/sp',
      'https://myapp.example.com/sp',
      'nonurl.example.com',
    ];
    
    const pattern = /^https?:\/\//i;
    
    urls.forEach(u => {
      console.log(u, '-->', u.replace(pattern, ''));
    });

    reply
    0
  • Cancelreply