Define a search pattern that can be used to search for things in a string.
.test( . . . )
: returns true or falsematch( . . . )
: returns the match/patternToLookFor/ig
:i
andg
at the end are flags meaning can insensitiveness and every occurrence, respectively..
: match anything with a wildcard period[ ]
: match single characters with multiple possibilities[a-z]
: match all lowercased letters of the alphabet[^0-9]/ig
: the^
inside brackets matches singles characters not specified. For example, this won't match any number in the entire string, no matter the case.+
: one or more times*
: zero or more times^word
: matchesword
at the beginning of the string. Notice it's without the brackets []word$
: the $ matches the case at the end of the string\w
: every letter and number\W
: every non letter and number\d
: all numbers\D
: all non numbers\s
: all whitespaces\S
: all non whitespaces{ , }
: curly brackets for quantity identifiers. It can be inserted a lower bound number and/or upper bound number of characters, or even only one number telling the exact number of characters
- Using regex to check if a username matches the following requirements:
- if there are numbers, they MUST be at the end
- letters can be lowercase and uppercase
- at least two characters long. Two-letter names can't have numbers
A possible pattern would be:
/^[A-Za-z]{2,}\d*$/
- Using capture groups (written inside parentheses) and the replace method to invert the order of a word:
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1')
returns"Camp Code"