Validate Tutorialspoint URL via JavaScript regex?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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
raja
Updated on 09-Nov-2020 07:31:44

Advertisements