I'd like to whitelist domains and I have this list as
(domain1|domain2)
However, it will still match to
ci.domain1
/
I'm writing the code in node.js
Here's the code
new RegExp('(domain1|domain2)', 'igm').test('ci.domain1');
I'd like to whitelist domains and I have this list as
(domain1.|domain2.)
However, it will still match to
ci.domain1.
https://regex101./r/A2IOJE/1/
I'm writing the code in node.js
Here's the code
new RegExp('(domain1.|domain2.)', 'igm').test('ci.domain1.');
Share
Improve this question
asked Sep 28, 2017 at 20:07
toytoy
12.2k29 gold badges101 silver badges180 bronze badges
0
2 Answers
Reset to default 6You just need to add ^
(start of string) & $
(end of string):
/^(domain1.|domain2.)$/
console.log(
new RegExp('^(domain1.|domain2.)$', 'igm').test('ci.domain1.'),
new RegExp('^(domain1.|domain2.)$', 'igm').test('domain1.'),
new RegExp('^(domain1.|domain2.)$', 'igm').test('domain2.')
)
With anchors and optional www
matching at the start you can use this regex:
/^(?:www\.)?(?:domain1|domain2)\.$/i
Also dot before needs to be escaped to avoid matching any character.
RegEx Demo
RegEx Breakup:
^
: Start(?:www\.)?
: Match optionalwww
at the start of domains(?:domain1|domain2)
: Matchdomain1
ordomain2
\.
: Match literal text.
$
: End