Languages
[Edit]
EN

JavaScript - validate encoded URI component characters

5 points
Created by:
Giles-Whittaker
379

In this short article, we would like to show how to check if some text is correctly encoded URI component using JavaScript.

Note: to know what characters should be escaped encoding URI component check this article.

Quick solution: 

// ONLINE-RUNNER:browser;

const VALIDATION_EPRESSION = /^[0-9a-zA-Z-_.!~*'()%]*$/;

const validateURIComponent = (text) => {
  	VALIDATION_EPRESSION.lastIndex = 0;
	return VALIDATION_EPRESSION.test(text);
};


// Usage example:

console.log(validateURIComponent("abcdefghijklmnopqrstuvwxyz"));  // normal English letters
console.log(validateURIComponent("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));  // capital English letters
console.log(validateURIComponent("-_.!~*'()"));                   // not escaped special characters
console.log(validateURIComponent("%"));                           // escape character

console.log(validateURIComponent("@#$^&+=[]"));   // should be escaped
console.log(validateURIComponent("ąćęłńóśźż"));   // should be escaped
                                                  // etc.

See also

  1. JavaScript - encode / escape URL characters

  2. JavaScript - decode url characters

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join