Skip to content

Commit

Permalink
添加检测中文字 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sofish committed Oct 8, 2012
1 parent 37166dd commit 20b60e0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
./plugins/AntBuild/tmp/
*.swp
*.D_Store
*.D_Store
.idea
42 changes: 42 additions & 0 deletions solutions/detect-chinese/detect-chinese.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>localStorage</title>
<style type="text/css">
body{padding:30px;}
p{margin-bottom:15px;}
#tips small{font-size:12px;color:#999;}
</style>
</head>

<body>

<div id="wrapper" class="single">

<h3>是否为中文?</h3>
<input id="words" placeholder="请输入中文字符">
<p id="tips"></p>

<script type="text/javascript">
var words = document.getElementById('words'),
tips = document.getElementById('tips'),
isChinese = function(word) {
return /[\u4E00-\uFA29]+|[\uE7C7-\uE7F3]+/.test(word)
}

words.onkeyup = function(){
var arr = [], msg = [],
input = words.value
for(var i=0,len=input.length;i<len;i++) arr.push(input[i])
for(var i=0,len=arr.length;i<len;i++){
!isChinese(arr[i]) && msg.push((i+1) + '<small>(' + arr[i] + ')</small>')
}
if(msg.length) tips.innerHTML = '第' + msg.join('、') + '个字不是中文';
}
</script>

</div><!-- #wrapper -->

</body>
</html>
9 changes: 9 additions & 0 deletions solutions/detect-chinese/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### 检测文字是否为中文

```js
var isChinese = function(word) {
return /[\u4E00-\uFA29]+|[\uE7C7-\uE7F3]+/.test(word)
}

isChinese(''); // true
```

0 comments on commit 20b60e0

Please sign in to comment.