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"
You can use regex
:
baseUrl.match(/\.(.*?)\.co/i)[1];
Regex Explanation
/
: Delimiters of regex
\.
: Matches .
literal(need to be escaped)()
: Capturing group.*?
: Match any stringco
: Matches string co
i
: Match in-case-sensitive[1]
: Get the capturing groupRegex Visualization
You can split strings at certain chars with split("."); (see http://www.w3schools.com/jsref/jsref_split.asp) then you can compare the results to your predefined words.
or you take the 2nd element of the results which would usually(?) be what you are looking for.
Take a look to this free library: http://medialize.github.io/URI.js/.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With