Skip to content

Instantly share code, notes, and snippets.

@Angelfire
Last active September 30, 2021 01:16
Show Gist options
  • Save Angelfire/6ea4c1eca8ffac0397d4ec0691ae9b5c to your computer and use it in GitHub Desktop.
Save Angelfire/6ea4c1eca8ffac0397d4ec0691ae9b5c to your computer and use it in GitHub Desktop.
Regex

Regex

Match literal strings

/Andres/

Match a literal string with different posibilities

/yes|no/

Match anything with wilcard period

/.un/ => run, sun, fun

Match single character with multiple posibilities

/b[aiu]g/ => bag, big, bug

Match letters of the alphabet

/[a-c]at/ => aat, bat, cat

Match numbers and letters

/[a-z0-9]/

Match single characters not specified

/[^aeiou]/

Match characters that occur one or more times

/a+/

Match characters that occur zero or more times

/go*/

Find characters with lazy matching

/<h?1>/ => <h1>

Match beginning string patterns

/^Andres/

Match ending string patterns

/Andres$/

Match all letters and numbers

/\w/ === /[A-Za-z0-9]+/

Match everything but letters and numbers

/\W/ === /[^A-Za-z0-9]/

Match all numbers

/\d/ === /[0-9]/

Match all non-numbers

/\D/ === /[^0-9]/

Match whitespace

/\s/

Match non-whitespace characters

/\S/

Specify upper and lower number of matches

/Oh{3,5}\sno/ => Ohhh no, Ohhhh no, Ohhhhh no

Specify only the lower number of matches

/Oh{4,}\sno/ => Ohhhh no, Ohhhhhhhhhhhhhhh no

Specify exact number of matches

/Oh{3}/ => Ohhh

Check for all or none

/favou?rite/

Positive and Negative Lookahead

A positive lookahead will look to make sure the element in the search pattern is there, but won't actually match it. A positive lookahead is used as (?=...) where the ... is the required part that is not matched.

A negative lookahead will look to make sure the element in the search pattern is not there. A negative lookahead is used as (?!...) where the ... is the pattern that you do not want to be there. The rest of the pattern is returned if the negative lookahead part is not present.

Match passwords that are greater than 5 characters long, and have two consecutive digits.

/(?=\w{6})(?=\w*\d{2})/

Simple password checker that looks for between 3 and 6 characters and at least one number.

/(?=\w{3,6})(?=\D*\d)/

Check for mixed grouping of characters

/P(engu|umpk)in/

Reuse Patterns using capture groups

Match a string that consists of only the same number repeated exactly three times separated by single spaces.

/^(\d+)\s\1\s\1$/

Use Capture Groups to search and replace

Write a regex fixRegex using three capture groups that will search for each word in the string one two three. Then update the replaceText variable to replace one two three with the string three two one and assign the result to the result variable. Make sure you are utilizing capture groups in the replacement string using the dollar sign ($) syntax.

let str = "one two three";
let fixRegex = /(one)\s(two)\s(three)/; // Change this line
let replaceText = "$3 $2 $1"; // Change this line
let result = str.replace(fixRegex, replaceText);

Flags

Ignore case while matching

/ignorecase/i

Find more than the first match

/Repeat/g

Remove Whitespace from Start and End

let hello = "   Hello, World!  ";
let wsRegex = /^\s+|\s+$/g;
let result = hello.replace(wsRegex, '');

Restrict Possible Usernames

  • Usernames can only use alpha-numeric characters.
  • The only numbers in the username have to be at the end. There can be zero or more of them at the end. Username cannot start with the number.
  • Username letters can be lowercase and uppercase.
  • Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.
let username = "JackOfAllTrades";
let userCheck = /^[a-z][a-z]+\d*$|^[a-z]\d\d+$/i;
let result = userCheck.test(username);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment