regex - HTML5 pattern for IP address - how to modify pattern to ignore the "_" (underscore) character -
i have found following pattern validating syntax of ip address:
pattern="((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}$"
but need modify ignore _
characters in places. because use input mask (it removes _
after defocusing field bootstrap validator plugin sometime doesn't catch it).
so how can modify regex ignore _
characters?
example strings should valid:
192.168.88.1__ 192.168.88_.200 192.16_.88_.2__ 192.168.88.2
in order match optional characters (0 or more occurrences), need *
quantifier. so, match ip addresses _
after numbers, can add _*
after each number:
((^|\.)((25[0-5]_*)|(2[0-4]\d_*)|(1\d\d_*)|([1-9]?\d_*))){4}_*$
see demo
although, think can remove anchors safely since html5 pattern attribute anchored default. can change regex match ip more intelligently (without allowing dot before it):
(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)_*(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)_*){3}
see another demo
Comments
Post a Comment