Greediness and Laziness in Regular Expressions
Let's try on
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p><p>Atque sunt minus ipsa asperiores. At.</p>
/<p>.*<\/p>/g
will greedy load and select all
Putting
?
will lazy load it
/<p>.*?<\/p>/g
{min, max}
Matches min to max occurrences{min}
Matches min occurrences{min,}
Matches min or max occurrences
Example:
/\w{4,6}/g
--> matches words with 4 to 6 letters./\w\w{4,6}/g
--> matches words with 5 to 7 letters.
Lazy:
/\w{4,6}?/g
--> find a minimum of up to 4
/\w{4,6}?-/g
--> find a minimum of up to 4 but to match -
will go up to 6
Capturing groups
A part of a pattern can be enclosed in parentheses (...)
. This is called a “capturing group
”.
That has two effects:
- It allows getting a part of the match as a separate item in the result array.
- If we put a quantifier after the parentheses, it applies to the parentheses as a whole.
https://javascript.info/regexp-groups
Top Regular Expressions:
select all comments
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)
it will select all comments of the format
/******/
/* */
/*
any comment
*/
// any comment
<a\s+(?:[^>]*?\s+)?href=(["'])(.*?)\1
<a\s+(?:[^>]*?\s+)?href=(["'])(.*?)\1
it will select of the below format
<a href='any link'
<a href="any link"
Url checker
/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/g
/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/g
it will select any URL
Only letters and numbers
/[^A-Za-z0-9]+/g
/[^A-Za-z0-9]+/g
Date formate (yyyy-mm-dd)
/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/
/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/
Url validation
/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/gm
/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/gm
Match whole word for example below will match any word containing work
/\b(\w*work\w*)\b/g
/\b(\w*work\w*)\b/g
special characters check
/[!@#$%^&*(),.?":{}|<>]/g
/[!@#$%^&*(),.?":{}|<>]/g
Extract String Between Two STRINGS
/(?<=% of )(.*)(?= at )/g
/(?<=% of )(.*)(?= at )/g
From the below example, it will select 6.89MiB
which is between of
and at
Example:
[download] 32.3% of 6.89MiB at 1.46MiB/s ETA 00:07
Match anything enclosed by square brackets.
/\[.*?\]/g
/\[.*?\]/g
Blocking sites with unblocked games
/unblocked-*games/ig
/unblocked-*games/ig
Example:
https://sites.google.com/site/unblockedgames66/
https://sites.google.com/site/unblockedgames77/
https://sites.google.com/site/unblockedgames4me/
https://sites.google.com/site/unblockedgamesvevo/
https://online-unblocked-games.weebly.com/
/^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$/i
/^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$/i
Find a Substring within a string that begins and ends with parenthesis
/(\(.*?\))/
/(\(.*?\))/
Match anything after the specified
/\/fall-open-house\?.*/g
/\/fall-open-house\?.*/g
eg:
http://www.madisoncountryday.org/fall-open-house?utm_source=WI-Madison&utm_campaign=dd06954861
/\b(?!ignoreme|ignoreyou)\b\S+/g
/\b(?!ignoreme|ignoreyou)\b\S+/g
eg:
aaa ignoreme blabla fasdfdsa
bbb ignoreme ad
bbb ignoreyou ad
10-digit phone number with hyphens
/^\d{3}-\d{3}-\d{4}$/gm
/^\d{3}-\d{3}-\d{4}$/gm
Match if doesn't start with the string
/^(?!Doesn't start with this string.*$).*/g
/^(?!Doesn't start with this string.*$).*/g
eg:
Doesn't start with this string
Java Variable
/^[\$_a-zA-Z]+[\$_\w]*$/gm
/^[\$_a-zA-Z]+[\$_\w]*$/gm
RegEx for JSON
/\{.*\:\{.*\:.*\}\}/g
/\{.*\:\{.*\:.*\}\}/g
Find any word in a list of words
/\bTHING|\bITEM|\bFOO/g
/\bTHING|\bITEM|\bFOO/g
eg:
ITEM FOO THING
Regular Expression For Decimal Validation
/^(\d*\.)?\d+$/igm
/^(\d*\.)?\d+$/igm
eg:
5.1
234.23425
0.234
.6454
45
0.
/^[^\s@]+@[^\s@.]+\.[^\s@.]+$/g
/^[^\s@]+@[^\s@.]+\.[^\s@.]+$/g
Twitter handle
/^@?(\w+)$/g
/^@?(\w+)$/g