Documente Academic
Documente Profesional
Documente Cultură
#javascript
Testing a regex
Ignoring case
regexWithCharRange.test(catString); // true
regexWithCharRange.test(batString); // true
regexWithCharRange.test(fatString); // false
normalHi.match(zeroOrMoreOsRegex); // ["hi"]
happyHi.match(zeroOrMoreOsRegex); // ["hiiiiii"]
twoHis.match(zeroOrMoreOsRegex); // ["hii", "hii"]
bye.match(zeroOrMoreOsRegex); // null
Lazy Matching
testString.match(greedyRexex); // ["catast"]
testString.match(lazyRegex); // ["cat"]
startingStringRegex.test(emmaAtFrontOfString); // true
startingStringRegex.test(emmaNotAtFrontOfString); // false
startingStringRegex.test(emmaAtBackOfString); // true
startingStringRegex.test(emmaNotAtBackOfString); // false
Matching all letters and numbers
longHand.test(numbers); // true
shortHand.test(numbers); // true
longHand.test(myFavoriteColor); // true
shortHand.test(myFavoriteColor); // true
noAlphaNumericCharRegex.test(weirdCharacters); // true
noAlphaNumericCharRegex.test(alphaNumericCharacters); // false
Matching whitespace
Matching non-whitespace
excitedRegex.test(regularHi); // true
excitedRegex.test(mediocreHi); // true
excitedRegex.test(superExcitedHey); //false
excitedRegex.test(regularHi); // false
excitedRegex.test(mediocreHi); // true
excitedRegex.test(superExcitedHey); //false
excitedRegex.test(regularHi); // false
excitedRegex.test(bestHi); // true
excitedRegex.test(mediocreHi); //false
languageRegex.test(britishSpelling); // true
languageRegex.test(americanSpelling); // true
Sore eyes?
dev.to now has dark mode.
❤
"misc" section of your
settings
Emma Wedekind ✨
UX Engineer, bibliophile, & cat mom
+ FOLLOW
PREVIEW SUBMIT
Feb 19
Brian Kichler
I go cross-eyed dealing wit h regex, but t his is an excellent reference I'm definit ely coming back to.
I almost always head to regex101.com whenever pat t ern-mat ching st art s to devour my brain.
30 REPLY
Feb 20
Richard vK
4 REPLY
Feb 20
Saúl Blanco Tejero
RT
1 REPLY
Feb 19
Alex Lohr
Using t he | operator is a dangerous habit . You easily forget to guard yourself against mat ching
subst rings, for example consider t he following line:
And now what happens on localhost.mydevious.url ? The secure solut ion is to use ^ and $ to
gat e t he st art and end of t he t est ed st ring.
15 REPLY
Feb 25
Chris Watson
3 REPLY
Feb 26
Alex Lohr
4 REPLY
Feb 19
Nick Taylor
14 REPLY
Feb 19
Miloslav Voloskov
For me, Regex and Perl are write-only languages. I can't debug, edit or even read regex – I just
rewrit e it from scrat ch wit h some tool like RegExr or somet hing. For me, a person who can
memorise all t hat synt ax is definit ely a superhuman.
8 REPLY
Feb 19
Denys Séguret
Making code readable is half t he job of t he coder. It 's also t rue for regexes, which means you
have to look for ways to separat e part s and comment your regexes (or at least name t he
groups).
Fort unat ely, you can find solut ions in most languages. Here are t wo examples from some of my
OS codes:
In Javascript :
cmdArgRegex = miaou.lib("rex")`
([a-z ]*) // options (optional)
(-?\d*\s*d\s*[\d+-]+) // left rolldef (mandatory)
(?: // inequation (optional)
\s*
([<>=]+) // operator
\s*
([\w+-]+) // right rolldef or scalar
)?
\s*$
/i`;
(I t hink t here are JS libs to do t hat wit hout t aking my code, now)
In rust :
static ref RE: Regex = Regex::new(
r"(?x)
^
(?P<slash_before>/)?
(?P<pattern>[^\s/:]+)?
(?:/(?P<regex_flags>\w*))?
(?:[\s:]+(?P<verb>\S*))?
$
"
12 REPLY
Feb 21
Sebastijan Grabar
The funny t hing about t hese Regex cheat cheet s is no mat t er how many of t hem I put in my
bookmarks, I never end up using t hem.
8 REPLY
Feb 26
Precious adeyinka
Feb 19
George W Langham
I t end to not bot her learning regex, t he cases where it 's useful, I'll just go to a sit e like regexr.com
and t hen use t heir tools to build one.
6 REPLY
Feb 20
Jackson Elfers
In t hink t hat 's t he general consensus. I only use it occasionally so it 's not wort h my t ime to be
100 percent fluent .
3 REPLY
Feb 20
Luis M Alvarez Pacheco
Anot her cheat sheet for my privat e gist collect ion lol
5 REPLY
Feb 26
mtomasek2
I'm using regex debuger, regexbuddy.com/, to debug different regex flavors. It saved me a lot of
t ime over t he years. It can also run on linux (using Wine).
They also have a tool for building regexps, regexmagic.com/, but I'm not using is so I'm just saying
it is out t here.
3 REPLY
Feb 25
Leonardo Galante
Great cheat sheet , t hank you Emma! Soon I'm gonna make a post about t his ext ension I made for
VSCode t hat could help underst anding RegExp: market place.visualst udio.com/it ems...
3 REPLY
Feb 26
Phillip Dodd
Oh my, I wish t hat I had encount ered t his post about t wo mont hs ago before I began t he difficult
journey of acquiring what (lit t le) regex knowledge I have now... haha!
I'm absolut ely going to be bookmarking t his to revisit next t ime I have t he need and will be sharing
it wit h my t eam. You've laid t his out in a very underst andable way; t hanks for sharing :)
3 REPLY
5 REPLY
Apr 30
Guillaume Duchemin
The result of mat ch is // ["E", "m", "m", "a", "1", "9", "3", "8", "2"] ;)
3 REPLY
Feb 20
I'mAHopelessDev
4 REPLY
Feb 20
plainJavaScript
Pret t y fine.
4 REPLY
Feb 19
Mehdi Vasigh
Great post and doubles as a great pract ice assessment if you hide all of t he code blocks!
4 REPLY
Feb 20
Bartosz Maciejewicz
Feb 19
Sohail Nasir
4 REPLY
Jun 20
Phil Ashby
Nicely done :)
Always choose carefully when choosing to use a regular expression of course. Our primary
applicat ion at work is for input validat ion, drawing heavily on t he excellent work done in OWASP:
owasp.org/index.php/OWASP_ Validat i...
1 REPLY
Feb 25
mmeyers00
3 REPLY
Feb 23
Lundeee
Never ever did i wrot e single regex wit hout some kind of cheat sheet or tool. Thank you.
3 REPLY
Feb 20
Saúl Blanco Tejero
I love regexp 😊
3 REPLY
Feb 19
monsterooo
⛄
3 REPLY
Feb 25
10secondsofcode
Nicely explained....!
2 REPLY
Feb 27
LuisPa García
2 REPLY
Feb 20
Jackson Elfers
There are definit ely some regex wizards out t here, great work put t ing t his toget her. 😁
2 REPLY
Feb 25
Shah Nawaz Shuvo
2 REPLY
Feb 22
Jon Randy
Best regex cheat sheet I ever came across - applet ree.or.kr/quick_ reference_ ca...
2 REPLY
Feb 21
Vinicius Dutra
2 REPLY
Feb 26
Tutlane
2 REPLY
Feb 25
UniWrighte
I've most ly used .t est , but you make good use of t he .mat ch met hod. Thanks for t he great regex
post wit h a JavaScript focus!
2 REPLY
Helping you choose the right languages and skills to maximize your value
563 33
A collection of smart and zany solutions to some common, and quite uncommon,
problems.
760 47
How to use `import/export` in Node without Babel
Corey Cleary - Oct 21