Skip to content

Instantly share code, notes, and snippets.

@sufiiiyan
Last active February 21, 2024 08:24
Show Gist options
  • Save sufiiiyan/4083f1b65506288ea84d1778882b3680 to your computer and use it in GitHub Desktop.
Save sufiiiyan/4083f1b65506288ea84d1778882b3680 to your computer and use it in GitHub Desktop.
Regex
  1. Useful regex
  2. Playground
  3. Resource
  4. Working url and number range

Useful regex

# Title regex Description/notes
1 Regex to allow alphanumeric, spaces, some special characters ^(?!\d+$)(?:[a-zA-Z0-9][a-zA-Z0-9 @&$]*)?$ not allowing only number
^(?!$)(?:[A-Za-z0-9][A-Za-z0-9 @&$,.?_-]*)?$ getting invalid character class due to -
^.*(?<!^\s)$ old used
^(?! )[\-a-zA-Z0-9 @&$,.?_]+(?! )$ working till now no space start end and special character
2 No space/null only alphabet and number with - only and no space between ^(?!\s)[a-zA-Z0-9-]*$
3 No space, no special char, no number for lang/city/etc ^(?:[a-zA-Z][a-zA-Z]*)?$
4 valid url /[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)/

My Regex search

5 Search vs code op=.*aws op= with aws in same line
6 Put cursor to end of each line vs code cmd+shft+L/ctr+alt+i
7 AppSec validation /<(script html

Playground

regex101
p2

Resource

websense
cheatography
rexegg


Rule Description
^The matches any string that starts with The
end$ matches a string that ends with end
^The end$ exact string match (starts and ends with The end)
roar matches any string that has the text roar in it
- -
abc* matches a string that has ab followed by zero or more c
abc+ matches a string that has ab followed by one or more c
abc? matches a string that has ab followed by zero or one
abc{2} matches a string that has ab followed by 2
abc{2,} matches a string that has ab followed by 2 more c
abc{2,5} matches a string that has ab followed by 2 up to 5
a(bc)* matches a string that has a followed by zero or more copies of the sequence bc
a(bc){2,5} match a string that has a followed by 2 up to 5 copies of the sequence bc
$^\*.[() All these symb has special meaning need to escape by \ if comes after some symb
. One character
a+ One or more occurrences of a
a* Zero or more occurrences of a
a? Zero or one repetition of a
Expression Description Example

Basic set

foo.bar represents single character fooabar,fooxbar
foo.*bar zero or more ocuurance foobar,fooabar,fooxxbar,fooabcbar
foo\s*bar \s represent single space foo bar,foo bar,foo bar
[fcl]oo Character class restricted to fcl foo,coo,loo
[^fcl]oo Any character except fcl moo,zoo,koo
[j-m]oo Character range from j to m moo,joo,koo
[j-mz]oo Character range from j to m and z moo,joo,koo,loo,zoo
[j-mJ-Mz]oo Character range from j to m and z Moo,joo,koo,Loo,zoo
x*[.#&]y* No need to escape period in square bracket..escape added for md file for or symbol x.y,xx#yyy,x&y
^foo.* start with foo after that any thing foo bar,fooadfvfv,foo bar baz
.*bar$ End with bar before that any thing foo baz bar,baz foo bar
x*[\^\-]y* ^ and - has special meaning so need to escape in bracket
x*[\\]y* \ need to be escaped anywhere present

Extended set

^[0-9]{3}$ a{m}Ex.^[0-9][0-9][0-9]$This number signifies the number of repetitions.So a followed by the number 'm' in curly braces means exactly 'm' repetitions of the letter 'a'. 321,345,999
^[a-z]{4,6}$ So m and n denote the range of repetitions So if we say a, it represents at least 'm' repetitions of the letter 'a' and at most 'n' repetitions of the letter 'a'. lion,tiger,cuckoo
(ha){4,} Parantheses are used to treat a group of characters as a single entity. We will enclose the 'h' and 'a' together in parantheses. So when the regex engine sees the curly brace repeater, it will take whatever is inside the parantheses and apply the repetition to it.So this means at least 4 repetitions of 'ha' should be present.The maximum repetitions coulde be anything. hahahaha, hahahahaha, hahahahaha
fooa+bar One or more occurrences of a fooabar,fooaabar
http?s://website Zero or one repetition of s http://website,https://website
(log|ply)wood log or ply logwood, plywood

Search and replace


  1. Caret is used as a negation symbol,but ONLY if it is within square brackets or within a character class.
  2. $ is a placeholder that signifies the end of a line.

Working url and number range

Working regex for url
let regexUrlWorking = /^((ftp|http|https):\/\/)?(www.)?(?!.*(ftp|http|https|www.))[a-zA-Z0-9_-]+(\.[a-zA-Z]+)+((\/)[-a-zA-Z0-9\@\:\%\_\+\*\.\~\#\?\&\/\/\=]+)?\/?$/
let finalRegexWorking = /^((ftp|http|https):\/\/)?(www.)?(?!.*(www.))[a-zA-Z0-9_-]+(\.[a-zA-Z]{2,})+((\/)[-a-zA-Z0-9\@\:\%\_\+\*\.\~\#\!\?\&\/\/\=]+)?\/?$/

Numeric range

^$|^([0-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|1[0-4][0-9][0-9][0-9]|15000)$ //=>0-15000
^$|^([1-9][5-9]|[2-9][0-9]|[1-3][0-9][0-9]|[4][0-7][0-9]|480)$ //=>15-480
^$|^([5-9]|[0-9][0-9]|[1-6][0-9][0-9]|[7][0-1][0-9]|720)$ //=>5-720

Note

Highlights information that users should take into account, even when skimming.

Important

Crucial information necessary for users to succeed.

Warning

Critical content demanding immediate user attention due to potential risks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment