I want to match either @ or 'at' in a regex. Can someone help? I tried using the ? operator, giving me /@?(at)?/ but that didn't work
To run a “whole words only” search using a regular expression, simply place the word between two word boundaries, as we did with ‹ \bcat\b ›. The first ‹ \b › requires the ‹ c › to occur at the very start of the string, or after a nonword character.
to combine two expressions or more, put every expression in brackets, and use: *? This are the signs to combine, in order of relevance: ?
Use square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.
A regular expression is a template or pattern used to find multiple different strings. Regular expressions can be used to identify groups of related URLs in access limiting filters and exceptions from these and as a more flexible form of a keyword to assign URLs to categories for blocking.
Try:
/(@|at)/
This means either @
or at
but not both. It's also captured in a group, so you can later access the exact match through a backreference if you want to.
/(?:@|at)/
mmyers' answer will perform a paren capture; mine won't. Which you should use depends on whether you want the paren capture.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With