Validate Tutorialspoint URL via JavaScript regex?


To validate a specific URL, use regular expression.

Example

The code is as follows −

function validateSoundURL(myURL) {
   var regularExpression = /^https?:\/\/(tutorialspoint\.com)\/(.*)$/;
   return myURL.match(regularExpression) && myURL.match(regularExpression)[2]
}
console.log(validateSoundURL("https://tutorialspoint.com/index"));
console.log(validateSoundURL("https://tutorialspoint.com/java"));

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo259.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo259.js
index
java

Updated on: 09-Nov-2020

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements