Skip to content
Advertisement

Capture word after first dot and before second dot

i have some strings like:

test.router.router1.ping 
test.router.hp.upload
demo.firewall.router.ping

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

[.router.]

but it match every .router. in my string, so also the third one pass.

how can i do that? Thank you

Answer

You may use

^[^.]*.router.

See the regex demo and the regex graph:

enter image description here

Details

  • ^ – start of string
  • [^.]* – 0+ chars other than .
  • .router. – a .router. substring.

JS demo:

var strs = [ 'test.router.router1.ping', 'test.router.hp.upload', 'demo.firewall.router.ping']
var rx = /^[^.]*.router./;
for (var i=0; i<strs.length; i++) {
  console.log(strs[i], '=>', rx.test(strs[i]))
}
Advertisement