JavaScript: Return true if the given string is an absolute URL, false otherwise
JavaScript fundamental (ES6 Syntax): Exercise-207 with Solution
Write a JavaScript program that returns true if the given string is an absolute URL, false otherwise.
- Use RegExp.prototype.test() to test if the string is an absolute URL.
Sample Solution:
JavaScript Code:
//#Source https://bit.ly/2neWfJ2
const isAbsoluteURL = str => /^[a-z][a-z0-9+.-]*:/.test(str);
console.log(isAbsoluteURL('https://google.com')); // true
console.log(isAbsoluteURL('ftp://www.myserver.net')); // true
console.log(isAbsoluteURL('/foo/bar')); // false
Sample Output:
true true false
Flowchart:

Live Demo:
See the Pen javascript-basic-exercise-207-1 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus
Previous: Write a JavaScript program to check if a given string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).
Next: Write a JavaScript program to check whether the provided value is of the specified type.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join