- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to test if a URL string is absolute or relative - JavaScript?
To test for a URL string, use regular expression.
Example
Following is the code −
var regularExpressionForURL = /^https?:\/\//i; var originalURL1 = "https://www.example.com/index"; var originalURL2 = "/index"; if (regularExpressionForURL.test(originalURL1)) { console.log("This is absolute URL"); } else { console.log("This is relative URL"); } if (regularExpressionForURL.test(originalURL2)) { console.log("This is absolute URL"); } else { console.log("This is relative URL"); }
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo266.js
Output
This will produce the following output on console −
PS C:\Users\Amit\javascript-code> node demo266.js This is absolute URL This is relative URL
- Related Articles
- Difference between an Absolute URL and a Relative URL
- How to test if a letter in a string is uppercase or lowercase using javascript?
- How to test String is null or empty?
- How to check if a string is a valid URL in Golang?
- JavaScript: How to Check if a String is a Literal or an Object?
- How to link pages using relative URL in HTML?
- CSS Absolute and Relative Units
- How to link pages using absolute URL in HTML?
- How to test if a parameter is provided to a function in JavaScript?
- How to check if an URL is valid or not using Java?
- Absolute and Relative Imports in Python
- Absolute and Relative frequency in Pandas
- Understanding CSS Absolute and Relative Units
- Absolute and Relative Magnetic Permeability (µ)
- How to test if a JavaScript cookie has expired?

Advertisements