Last active
March 17, 2022 07:40
-
-
Save neos1803/6229bc017de9addf3717f02c32cc1a80 to your computer and use it in GitHub Desktop.
Example of Characters Constraint Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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