Regular expressions for Windows and Linux paths

I searched a lot of regular expressions for Windows and Linux on the Internet, but found that they didn’t work, so I had to write a
Windows path regular expression by myself:
Example: C:\Program Files\

^[a-zA-z]:\\([\u4E00-\u9FA5A-Za-z0-9_\s]+\\{
    
    1})+$

Regular expression of Linux path:
Example: /user/bin/

^\/([\u4E00-\u9FA5A-Za-z0-9_]+\/{
    
    1})+$

If you want the suffix '/' or '\' to be optional, just change the last {1} to ?.

Note:
When using JavaScript's regular test method, pay attention to the escape of \ inside.
For example, to test: C:\Program Files

/^[a-zA-z]:\\([\u4E00-\u9FA5A-Za-z0-9_\s]+\\{1})+$/.test("C:\\Program Files\\")

If the string of the form is obtained directly, there is no need to consider / escape issues.

Guess you like

Origin blog.csdn.net/weixin_43589827/article/details/118145752