Most Useful common Regex collection


The most commonly used and useful regexes collection.

1) Trim trailing spaces
=> ^[\s]*(.*?)[\s]*$

2) Matches any valid HTML tag. (Not for nested tags)
=> <([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)

3) Check for Hexadecimal value
=> \B#(?:[a-fA-F0–9]{6}|[a-fA-F0–9]{3})\b

4) Validate email ID
=> \b[\w.!#$%&’*+\/=?^`{|}~-]+@[\w-]+(?:\.[\w-]+)*\b

5) Username validation
Minimum length of 3, maximum length of 16, composed by letters, numbers or dashes.
=> /^[a-z0-9_-]{3,16}$/

6) Password checking
Moderate password - Minimum length of 6, at least one uppercase letter, at least one lowercase letter, at least one number, at least one special character
=> (?=^.{6,}$)((?=.*\w)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[|!"$%&\/\(\)\?\^\'\\\+\-\*]))^.*

Strong password - Should have 1 lowercase letter, 1 uppercase letter, 1 number, 1 special character and be at least 8 characters long
=> /(?=(.*[0-9]))(?=.*[\!@#$%^&*()\\[\]{}\-_+=~`|:;"'<>,./?])(?=.*[a-z])(?=(.*[A-Z]))(?=(.*)).{8,}/

7) URL (http, https or ftp) checking
If you want to use capturing groups to get scheme, path, etc. (or add user-info, host, port…)
=> ^(((https?|ftp):\/\/)?([\w\-\.])+(\.)([\w]){2,4}([\w\/+=%&_\.~?\-]*))*$

8) IPv4 address validation
=> \b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b

9) Alpha-numeric, literals, digits, lowercase, uppercase chars only
\w                //alpha-numeric only
[a-zA-Z]          //literals only
\d                //digits only
[a-z]             //lowercase literal only
[A-Z]             //uppercase literal only

10) find an image with a specific source
enter image name to search img tags with specific image
=> /<img.*src=".*IMAGE-NAME-HERE".*?>/
eg:- preg_replace('/<img.*src=".*'.basename($img_path).'".*?>/', 'REPLACE-WITH', $string);
- here i am replacing image tags where image name matches.

11) find and replace all image tags
=> /<img[^>]+\>/i

12) remove specific html tags
remove any html tag, mention html tag without brackets.
=> /<\\/?HTML-TAG-HERE(.|\\s)*?>/

13) get image source from <img tag>
=> /^<\s*img[^>]+src\s*=\s*(["'])(.*?)\1[^>]*>$/

14) remove junk characters from string
=> '/[^(\x20-\x7F)\x0A\x0D]*/'
eg:- preg_replace('/[^(\x20-\x7F)\x0A\x0D]*/','', $string);

15) remove white spaces from string
=> /\s+/
eg:- preg_replace('/\s+/', '-', $string);
- here i am replacing spaces with '-'

16) remove non-alphanumeric character from string
=> /[^A-Za-z0-9\-]/
eg:- preg_replace('/[^A-Za-z0-9\-]/', '-', $galleryURL)
- here i am replacing any non alphanumeric character with '-'

17) remove a specific attribute from tags.
=> / attribute=("|\')(.*?)("|\')/
eg:- preg_replace('/ style=("|\')(.*?)("|\')/','',$string);
here i am replacing style attribute from html tags.

18) remove <script> tag from string
=> #<script(.*?)>(.*?)</script>#is
eg:- preg_replace('#<script(.*?)>(.*?)</script>#is', '', $string);
- here i am removing script tags from html string

19) replace shortcode (useful for wordpress)
=> /\[SHORTCODE_HERE id="([^"]*)"]/

20) Javascript Handlers
Inline JS handler => /\bon\w+=\S+(?=.*>)/
Inline JS handler with element => /(?:<[^>]+\s)(on\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/

more to coming soon...

0 comments :