Skip to content

Instantly share code, notes, and snippets.

@neos1803
Last active March 17, 2022 07:40
Show Gist options
  • Save neos1803/6229bc017de9addf3717f02c32cc1a80 to your computer and use it in GitHub Desktop.
Save neos1803/6229bc017de9addf3717f02c32cc1a80 to your computer and use it in GitHub Desktop.
Example of Characters Constraint Regex
// Check string contains only one space
const pattern = new RegExp(/^\S+(?: \S+)*$/);
// Check if string is url
const pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i');
// Check if space is the only non-numeric-alphabet character
const pattern = new RegExp(/^$|.*\S+.*/);
// Check if string contains space
const pattern = new RegeExp(/^\S*$/);
// Check if string only has alphabet, dot and ' characters
const pattern = new RegeExp(/^[a-zA-Z .']+$/);
// Check if string only has numeric characters
const pattern = new RegeExp(/^\d+$/);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment