--- title: RegExp.prototype.test() slug: Web/JavaScript/Reference/Global_Objects/RegExp/test tags: - JavaScript - Method - Prototype - RegExp - Regular Expressions translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/test --- <div>{{JSRef}}</div> <p><code><strong>test()</strong></code> 方法执行一个检索,用来查看正则表达式与指定的字符串是否匹配。返回 <code>true</code> 或 <code>false</code>。</p> <div>{{EmbedInteractiveExample("pages/js/regexp-prototype-test.html", "taller")}}</div> <div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div> <h2 id="Syntax" name="Syntax">语法</h2> <pre class="notranslate"><var>regexObj</var>.test(str)</pre> <h3 id="Parameters" name="Parameters">参数</h3> <dl> <dt><code>str</code></dt> <dd>用来与正则表达式匹配的字符串</dd> </dl> <h3 id="返回值">返回值</h3> <p>如果正则表达式与指定的字符串匹配 ,返回<code>true</code>;否则<code>false</code>。</p> <h2 id="Description" name="Description">描述</h2> <p>当你想要知道一个正则表达式是否与指定的字符串匹配时,就可以使用 <code>test()</code>(类似于<code> </code> {{jsxref("String.prototype.search()")}} 方法),差别在于test返回一个布尔值,而 search 返回索引(如果找到)或者-1(如果没找到);若想知道更多信息(然而执行比较慢),可使用{{jsxref("RegExp.prototype.exec()", "exec()")}} 方法(类似于 {{jsxref("String.prototype.match()")}} 方法)。 和 {{jsxref("RegExp.prototype.exec()", "exec()")}} (或者组合使用),一样,在相同的全局正则表达式实例上多次调用<code><code>test</code></code>将会越过之前的匹配。</p> <h2 id="Examples" name="Examples">示例</h2> <h3 id="Example_Using_test" name="Example:_Using_test">使用 <code>test()</code></h3> <p>一个简单的例子,测试 "hello" 是否包含在字符串的最开始,返回布尔值。</p> <pre class="brush: js notranslate">let str = 'hello world!'; let result = /^hello/.test(str); console.log(result); // true </pre> <p>下例打印一条信息,该信息内容取决于是否成功通过指定测试:</p> <pre class="brush: js notranslate">function testinput(re, str){ var midstring; if (re.test(str)) { midstring = " contains "; } else { midstring = " does not contain "; } console.log(str + midstring + re.source); } </pre> <h3 id="当设置全局标志的正则使用test">当设置全局标志的正则使用<code>test()</code></h3> <p>如果正则表达式设置了全局标志,<code>test() </code>的执行会改变正则表达式 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex" title="The lastIndex is a read/write integer property of regular expression instances that specifies the index at which to start the next match."><code>lastIndex</code></a>属性。连续的执行<code>test()</code>方法,后续的执行将会从 lastIndex 处开始匹配字符串,({{jsxref("RegExp.prototype.exec()", "<code>exec()</code>")}} 同样改变正则本身的 <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex" title="The lastIndex is a read/write integer property of regular expression instances that specifies the index at which to start the next match.">lastIndex</a>属性值</code>).</p> <p>下面的实例表现了这种行为: </p> <pre class="brush: js notranslate">var regex = /foo/g; // regex.lastIndex is at 0 regex.test('foo'); // true // regex.lastIndex is now at 3 regex.test('foo'); // false </pre> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('ES3')}}</td> <td>{{Spec2('ES3')}}</td> <td>Initial definition. Implemented in JavaScript 1.2.</td> </tr> <tr> <td>{{SpecName('ES5.1', '#sec-15.10.6.3', 'RegExp.test')}}</td> <td>{{Spec2('ES5.1')}}</td> <td></td> </tr> <tr> <td>{{SpecName('ES6', '#sec-regexp.prototype.test', 'RegExp.test')}}</td> <td>{{Spec2('ES6')}}</td> <td></td> </tr> <tr> <td>{{SpecName('ESDraft', '#sec-regexp.prototype.test', 'RegExp.test')}}</td> <td>{{Spec2('ESDraft')}}</td> <td></td> </tr> </tbody> </table> <h3 id="sect1"></h3> <h2 id="浏览器兼容性">浏览器兼容性</h2> <div class="hidden">这个页面的兼容性表格是由结构数据生成。如果你想对数据作出贡献,详情请前往 <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并给我们一个PR。</div> <p>{{Compat("javascript.builtins.RegExp.test")}}</p> <h2 id="Firefox特殊注意">Firefox特殊注意</h2> <p>在 Firefox 8之前, <code>test()</code> 被不正确地实现了;当无参数地调用时,它会匹配之前的输入值 (RegExp.input 属性),而不是字符串"undefined"。这已经被修正了;现在 <code>/undefined/.test()</code> 正确地返回<code>true</code>,而不是错误。</p> <h2 id="相关链接">相关链接</h2> <ul> <li>在<a href="/zh-CN/docs/Web/JavaScript/Guide" title="JavaScript/Guide">JavaScript指南</a>的<a href="/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions" title="JavaScript/Guide/Regular_Expressions">正则表达式</a>章节</li> <li>{{jsxref("RegExp")}}</li> </ul>