问题
I have a input field for Linux mount path in my form I need regex to validate following cases :
Should not allow user to enter
/, /bin, /dev, /etc, /mnt, /opt, /run,/srv, /sys, /tmp, /usr, /var, /lib, /proc, /sbin, /root, /boot, /home, /lib64, /media, /opt/folde r, /opt/folder/, /usr/bin, /usr/lib, /usr/lib64, /usr/sbin
Should allow user to enter
/opt/folder, /var/folder, /tmp/folder
/bin/foldername, /boot/foldername, /devfoldername, /etc/foldername, /home/foldername, /lib/foldername, /lib64/foldername, /media/foldername, /mnt/foldername, /opt/foldername, /proc/foldername, /root/foldername, /run/foldername, /sbin/foldername, /srv/foldername, /sys/foldername, /tmp/foldername, /usr/foldername, /var/foldername
i'm tring with this one: ^(.+)\/([^/\s]+)$
but it is working for negative case like below:
/, /bin, /dev, /etc, /mnt, /opt, /run,/srv, /sys, /tmp, /usr, /var, /lib, /proc, /sbin, /root, /boot, /home, /lib64, /media, /opt/folde r, /opt/folder/
positive case like below:
/bin/foldername, /boot/foldername, /devfoldername, /etc/foldername, /home/foldername, /lib/foldername, /lib64/foldername, /media/foldername, /mnt/foldername, /opt/foldername, /proc/foldername, /root/foldername, /run/foldername, /sbin/foldername, /srv/foldername, /sys/foldername, /tmp/foldername, /usr/foldername, /var/foldername
please can anyone here help me out thanks
回答1:
Try with this regex: ^(\/[a-zA-Z0-9_-]+){2,}$(?<!^(\/usr\/bin)|(\/usr\/lib)|(\/usr\/lib64)|(\/usr\/sbin)$)
Look demo here
来源:https://stackoverflow.com/questions/61494926/regex-for-linux-mount-path-to-validate-user-enter-path-value-in-java-script