Posted 26 Mar 2022 | by anythingultimate
JavaScript Regular Expression Part 4

  



Previous article 

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





specifying repetition amount

{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])))*\*+/)|(//.*)

it will select all comments of the format

/******/

/* */

/*
 any comment
*/

// any comment 



Find href

<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

it will select any URL

Only letters and numbers

/[^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]))/

Url validation

/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/gm

Match whole word for example below will match any word containing work

/\b(\w*work\w*)\b/g

special characters check

/[!@#$%^&*(),.?":{}|<>]/g

Extract String Between Two STRINGS

/(?<=% 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

Blocking sites with unblocked games

/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/



Simple date dd/mm/yyyy

/^([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

eg:
http://www.madisoncountryday.org/fall-open-house?utm_source=WI-Madison&utm_campaign=dd06954861



all except word

/\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

Match if doesn't start with the string

/^(?!Doesn't start with this string.*$).*/g

eg:
Doesn't start with this string

Java Variable

/^[\$_a-zA-Z]+[\$_\w]*$/gm

RegEx for JSON

/\{.*\:\{.*\:.*\}\}/g

Find any word in a list of words

/\bTHING|\bITEM|\bFOO/g

eg:
ITEM FOO THING

Regular Expression For Decimal Validation

/^(\d*\.)?\d+$/igm

eg:
5.1
234.23425
0.234
.6454
45
0.



Email

/^[^\s@]+@[^\s@.]+\.[^\s@.]+$/g

Twitter handle

/^@?(\w+)$/g

Liked This Page. Spare a while to share it.

About The Admin Of This Blog:

Author Of This Article

I am a passionate and experienced Full Stack Web Developer having 4+ years of experience in Web Development using Laravel, React, WordPress, Angular, Vue, Bootstrap, Tailwind CSS, Saas, ES6, etc. I like to explore and learn about new technologies whenever I get any chance.

Stay Connected With Me On GooglePlus, Facebook And Twitter

Calculator

C
±
x ²
%
7
8
9
*
(
4
5
6
/
)
1
2
3
-
+
0
.
=
x ²
x^
sin
cos
tan
x !
π
C
log
ln
e
rad
7
8
9
*
(
4
5
6
/
)
1
2
3
-
+
0
.
%
±
=

Calender

Sa
Su
Mo
Tu
We
Th
Fr

Popular Posts

Total Blog Views