---
title: "正则表达式判断是否是合法路径"
author: "guoweimail"
date: "2012-02-17T11:21:44Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2012-02-21T10:24:10Z"
id: 1409305
url: "https://mrwillgo.iteye.com/blog/1409305"
---

```

```

 

在页面前端使用一个文本框，输入条件是：c:test

 

在java中File判断是可以通过的，可以创建的

 

 

```
File file = new File(filePath);
if (!file.exists()) {
     file.mkdir();
} 
```

 

 

说明是一个合法的路径。

 

 

但是在windows中，如果打开我的电脑，然后输入c:test，会报错“windows找不到xxxx，请检查拼写并重试”

 

采用正在表达式可以解决这一问题：

 

代码如下：

 

```
function testPath() {
	var path = /^[a-zA-Z]:[\\]((?! )(?![^\\/]*\s+[\\/])[\w -]+[\\/])*(?! )(?![^.]*\s+\.)[\w -]+$/;
	var backuppath = document.getElementById("xxx");
	if (path.test(backuppath)) {
		return true;
	} else {
		alert("请填写正确路径!");
		return false;
	}
	return true;
}
```