xxxxxxxxxx
<!DOCTYPE html>
<html>
<title>JavaScript search() 方法 - 基础教程(div.cn)</title>
<body>
<h1>JavaScript search() 方法</h1>
<p>单击按钮搜索字符串" be be",并显示第一个匹配项的位置。</p>
<button onclick="myFunc()">Click</button>
<p id="result"></p>
<script>
function myFunc() {
var str = 'The question is to be, or not to be, that is to be.';
var pos = str.search('to be');
document.getElementById('result').innerHTML = pos;
}
</script>
</body>
</html>