`
webcode
  • 浏览: 5955617 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

正则表达式提取字符串

 
阅读更多

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>javaScript正则表达式提取字符串中字母、数字、中文</title>

</head>
<body>

<input type="text" id="oText" value="4000没dfgdffg拭找34554imiadf测拭3453534" size=100><br/>
<input type="button" value="提取数字" onclick="window.alert(self['oText'].value=self['oText'].value.replace(/[^\d]/g,''));">
<input type="button" value="提取中文" onclick="window.alert(self['oText'].value=self['oText'].value.replace(/[^\u4E00-\u9FA5]/g,''));">
<input type="button" value="提取英文" onclick="window.alert(self['oText'].value=self['oText'].value.replace(/[^a-zA-Z]/g,''));">
<input type="button" value="还原" onclick="self['oText'].value='4000没dfgdffg拭找34554imiadf测拭3453534';">
<br>
<input name="s" size=100 value="4000没dfgdffg拭找34554imiadf测拭3453534sfgsf哈哈哈"><br/>
<input type="button" value="TEST" onclick="
var n = s.value.match(/\d/g);
var e = s.value.match(/[a-z]/ig);
var c = s.value.match(/[^ -~]/g);
alert(n == null ? '没有数字' : '数字有 ' + n.length + ' 个,是:' + n.join('、'));
alert(e == null ? '没有字母' : '字母有 ' + e.length + ' 个,是:' + e.join('、'));
alert(c == null ? '没有中文' : '中文有 ' + c.length + ' 个,是:' + c.join('、'));
">
</body>
</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics