Useful RegEx patterns

Hi Friends,

Today I am not writing something about the SharePoint, but this will be more useful for the client side development. So in many scenarios we need to validate the input string eg valid email address and more.



I have found some of the common used validations which we can validate using the regular expressions. I'll be adding more to these as I encounter more and feel free to let me know if you need any more.



Commonly used Regular Expressions
Title Description Regular Expression
Date Time Format Regular expression will match the dates with dashes or slashes or spaces. eg: dd-mm-yyy or dd/mm/yyy and optional time separated by space or dash eg: dd-mm-yyy-hh:mm:ss /^(0?[1-9]|[12][0-9]|3[01])([ \/\-])(0?[1-9]|1[012])\2([0-9][0-9][0-9][0-9])(([ -])([0-1]?[0-9]|2[0-3]):[0-5]?[0-9]:[0-5]?[0-9])?$/
24 hour time format Regular expression to matches time in 24 hour format /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/
ISO 8601 format for Date and Time Regular expression will match the valid ISO-8601 date and time format /^(?![+-]?\d{4,5}-?(?:\d{2}|W\d{2})T)(?:|(\d{4}|[+-]\d{5})-?(?:|(0\d|1[0-2])(?:|-?([0-2]\d|3[0-1]))|([0-2]\d{2}|3[0-5]\d|36[0-6])|W([0-4]\d|5[0-3])(?:|-?([1-7])))(?:(?!\d)|T(?=\d)))(?:|([01]\d|2[0-4])(?:|:?([0-5]\d)(?:|:?([0-5]\d)(?:|\.(\d{3})))(?:|[zZ]|([+-](?:[01]\d|2[0-4]))(?:|:?([0-5]\d)))))$/
HTML tags Regular expression to match opening and closing HTML tags with content between /^<([a-z1-6]+)([^<]+)*(?:>(.*)<\/\1>| *\/>)$/
Password Regular expression to validate string between 3 and 16 characters, allowing alphanumeric characters and hyphens and underscores /^[a-zA-Z0-9_-]{3,16}$/
Hex Color Value Regular expression to validate the hex color /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
Email Regular expression to verify if there is '@' in the email address mentioned /^.+@.+$/
Source of an image Regular expression which matches the src attribute of an image HTML tag /^<\s*img[^>]+src\s*=\s*(["'])(.*?)\1[^>]*>$/
Positive Integer Regular expression for positive integer /^\d+$/
Negative Integer Regular expression for negative integer /^-\d+$/
Integer Regular expression for integer /^-?\d+$/
Number Regular expression for Integer or float which are positive as well as negative /^-?\d*\.?\d+$/
Positive Number Regular expression for Integer or float which are positive /^-\d*\.?\d+$/
Negative Number Regular expression for Integer or float which are negative /^\d*\.?\d+$/
New Line Regular expression for new line in the text /[\r\n]|$/
CSS Comments Regular expression to match the standard CSS comments /\/\*[^*]*\*+([^\/*][^*]*\*+)*\//


Happy Validating
- Sumit Kanchan

Comments

Popular posts from this blog

SharePoint Framework (SPFx) : Cascade dropdown in webpart properties

Checking user permission in SharePoint Framework (SPFx) webpart

SharePoint Image Gallery