Why does this rule not match the following string?

var str = "https://23.segment.com/?usename=David";
var pattern = /^(http|https|ftp|mailto):\/\/[0-9a-z\.]+([\/0-9a-z]|[-\_\?=])*([\.a-z]*)$/
console.log(pattern.exec(str));//null

Why doesn"t the above regular match come out? ask God for advice

Mar.22,2021

recommend a online test regular website
remember to select JavaScript .
you can delete the latter first. Analyze it one by one.

at first glance, the string obviously has a David . There is no match for capital letters in the rule, so of course it doesn't.
you can match with a simple change.

^(http|https|ftp|mailto):\/\/[0-9a-z\.]+([\/0-9a-z]|[-\_\?=])*([\.a-zA-Z]*)$

https://regex101.com/
try it yourself.

Menu