Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  126] [ 1]  / answers: 1 / hits: 20105  / 9 Years ago, tue, july 7, 2015, 12:00:00

I have these strings:



baseUrl = http://www.example.com
baseUrl = https://secure.example-server.com:443


Can someone tell me how I can extract the server information from baseUrl so I can get either example and example-server


More From » regex

 Answers
11

You can use regex:



baseUrl.match(/.(.*?).co/i)[1];


Regex Explanation




  1. /: Delimiters of regex

  2. .: Matches . literal(need to be escaped)

  3. (): Capturing group

  4. .*?: Match any string

  5. co: Matches string co

  6. i: Match in-case-sensitive

  7. [1]: Get the capturing group



Regex Visualization



enter


[#65903] Saturday, July 4, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondaytond

Total Points: 92
Total Questions: 88
Total Answers: 96

Location: China
Member since Fri, Jan 15, 2021
3 Years ago
dylondaytond questions
Tue, Jun 22, 21, 00:00, 3 Years ago
Thu, May 7, 20, 00:00, 4 Years ago
;