Skip to content

Instantly share code, notes, and snippets.

@Aberratio
Last active February 8, 2023 14:20
Show Gist options
  • Save Aberratio/dac8687b568834fae1901907f957cecf to your computer and use it in GitHub Desktop.
Save Aberratio/dac8687b568834fae1901907f957cecf to your computer and use it in GitHub Desktop.
JavaScript regex

Example regexes

URL

^(https?:\\/\\/)?(www\\.)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$

E-mail

^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$

Phone number

^\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{4}$

Date YYYY-MM-DD

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$

Integers

^-?\d+$

Floating point numbers

^-?\d+(\.\d+)?$

Zip codes (in the US)

^\d{5}(?:[-\s]\d{4})?$

First names

^[A-Z][a-zA-Z]*$

Surnames

^[A-Z][a-zA-Z]*(-[A-Z][a-zA-Z]*)?$

Times in HH:MM:SS format

^(2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]$

Passwords with requirements (e.g., at least 8 characters, at least one capital letter, one number, one special character)

^(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$

HTML code

(<[a-z][\s\S]*>.*<\/[a-z][\s\S]*>)$

JavaScript code

^(?!\s*(?:var|let|const|function|if|for|while)\s+[\w\d_$]+\s*=\s*require\()(?:(?:\/\/.*\n?)*|\/\*[\s\S]*?\*\/)*\s*(?:var|let|const|function)\s+[\w\d_$]+\s*\(.*\)\s*\{[\s\S]*\}$

How to use it?

Check if it fits the pattern

Function will return true if the string given as a parameter is a URL and false if it is not.

const isUrl = (textToCheck) => {
  var pattern = new RegExp('^(https?:\\/\\/)?(www\\.)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$');
  return pattern.test(textToCheck);
}

Test it

console.log(isUrl('https://www.example.com')); // true
console.log(isUrl('false.com')); // false

Count the number of occurrences

The function using regex will return the number of repetitions of the word given as the first parameter of the function (word) in the sentence given as the second parameter of the function (sentence).

const countWordOccurrences = (word, sentence) => {
  var pattern = new RegExp('\\b' + word + '\\b', 'gi');
  return (sentence.match(pattern) || []).length;
}

In the above code, the regex pattern \\b used to determine the word boundary, and the g flag is used to search for all repetitions, and the i flag is used to ignore case.

Test it

console.log(countWordOccurrences('the', 'The quick brown fox jumps over the lazy dog')); // 2
console.log(countWordOccurrences('dog', 'The quick brown fox jumps over the lazy dog')); // 1

Check if it occurs

The function will return true if there is a URL anywhere in the text given as the function parameter.

const hasUrl = (text) => {
  var pattern = new RegExp('^(https?:\\/\\/)?(www\\.)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$');
  return pattern.test(text);
}

Test it

console.log(hasUrl('Check out https://www.example.com for more information')); // true
console.log(hasUrl('This text does not contain a URL')); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment