i have some strings like:
JavaScript
x
4
1
test.router.router1.ping
2
test.router.hp.upload
3
demo.firewall.router.ping
4
I’m trying to write a regex that makes the first two string pass, but not the third one. The rule is that if the string contains the word “router” after the first dot and before the second dot it’s ok.
i tried with
JavaScript
1
2
1
[.router.]
2
but it match every .router. in my string, so also the third one pass.
how can i do that? Thank you
Advertisement
Answer
You may use
JavaScript
1
2
1
^[^.]*.router.
2
See the regex demo and the regex graph:
Details
^
– start of string[^.]*
– 0+ chars other than.
.router.
– a.router.
substring.
JS demo:
JavaScript
1
5
1
var strs = [ 'test.router.router1.ping', 'test.router.hp.upload', 'demo.firewall.router.ping']
2
var rx = /^[^.]*.router./;
3
for (var i=0; i<strs.length; i++) {
4
console.log(strs[i], '=>', rx.test(strs[i]))
5
}