每天CookBook之JavaScript-007

  • 使用正则表达式查找子字符串
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>006</title>
</head>
<body>
	
</body>
<script type="text/javascript">
var searchString = "Now is the time, this is the tame";
var pattern = /t\w*e/g;
var matchArray;

var str = "";

while((matchArray = pattern.exec(searchString)) != null){
	str += "at " + matchArray.index + " we found " + matchArray[0] + "\n";
}
console.log(str);

var re = /a(p+).*(pie)/ig;
var result = re.exec("The apples in the apple pie are tart");
console.log(result);
console.log(result.index);
console.log(result.input);
</script>
</html>
posted @ 2016-07-09 11:01  4Thing  阅读(97)  评论(0编辑  收藏  举报