How can I extract text from the middle of a string with Javascript?

Samantha J T Star

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"

Tushar

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 image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I extract a formatted string from a PDF File name in JavaScript?

HOW can i extract text from SPAN? Please

How can I extract text fragments from PDF with their coordinates in Python?

Extract the middle names and last name from the string using regex of Javascript

How can I extract tuples from a string?

How can i extract data from String?

How can I extract text from string in python?

How can I remove something from the middle of a string with regex?

How do I extract specific parts of text from a string in R?

How to extract text from string?

How can I extract text from images?

How can I show a label with text in the middle?

How can I get 2 characters from the middle of a string?

How can I append text in the middle of a pipeline?

How can I extract data from text file?

How can I extract text before a character or string in a batch file?

How can i extract URL's from a piece of text into an Array using JavaScript

How can I extract text from a string using regex or substring in java?

How do I extract text and numbers from a string within a cell?

How can I extract these characters from a text file?

How can I extract a particular link from a string of HTML in JavaScript?

how can i select a specific string in RegExp From the middle

How can I increase a number in the middle of a string?

How can I extract number from text with regular expression

How can I extract the text from the <em> tag using BeautifulSoup

How can I extract hashtags from string?

Google Sheets: How can I extract partial text from a string based on a column of different options?

How can I extract words from a file from a string

How to extract more than one text from the string with javascript?