Hi Friends 👋,

Welcome To Infinitbility! ❤️

To validate absolute url string using regex, use /^(?:[a-z+]+:)?/// it will handle absolute url.

Let’s see short example of javascript regex for absolute url.

const regex = new RegExp('^(?:[a-z+]+:)?//');
console.log(regex.test("hello"))

Today, I’m going to show you How do I check value contain absolute url in javascript, as above mentioned, I’m going to use the above-mentioned regex with test() method.

Let’s start today’s tutorial how do you check absolute url in javascript using regex?

Here, I will show validate absolute url in javascript and typescript.

Javascript regex for absolute url example

Here, we will create common validate function where we will validate param should absolute url.

function validate(param){
  const regex = new RegExp('^(?:[a-z+]+:)?//');
  return regex.test(param);
}

// validate empty string
console.log(validate(""))
// false

// validate relative url
console.log(validate("/infinitbility/89/43"))
// false

// validate absolute url
console.log(validate("https://infinitbility.com/"))
// true

Output

Typescript regex for absolute url example

Same like javascript, we will create a function and call it with diffrent parameters.

function validate(param: string){
  const regex = new RegExp('^(?:[a-z+]+:)?//');
  return regex.test(param);
}

// validate empty string
console.log(validate(""))
// false

// validate relative url
console.log(validate("/infinitbility/89/43"))
// false

// validate absolute url
console.log(validate("https://infinitbility.com/"))
// true

Output

I hope it helps you, All the best 👍.