mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-09 23:44:06 +08:00
141 lines
9.4 KiB
HTML
141 lines
9.4 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<p><code><strong>test()</strong></code> 方法执行一个检索,用来查看正则表达式与指定的字符串是否匹配。返回 <code>true</code> 或 <code>false</code>。</p>
|
||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||
<pre><var>regexObj</var>.test(str)</code></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> <a href="Reference/Global_Objects/String/search" title="search() 方法执行正则表达式和 String对象之间的一个搜索匹配。"><code>String.prototype.search()</code></a> 方法),差别在于test返回一个布尔值,而 search 返回索引(如果找到)或者-1(如果没找到);若想知道更多信息(然而执行比较慢),可使用<a href="Reference/Global_Objects/RegExp/exec" title="exec() 方法在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null。"><code>exec()</code></a> 方法(类似于 <a href="Reference/Global_Objects/String/match" title="当一个字符串与一个正则表达式匹配时, match()方法检索匹配项。"><code>String.prototype.match()</code></a> 方法)。 和 <a href="Reference/Global_Objects/RegExp/exec" title="exec() 方法在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null。"><code>exec()</code></a> (或者组合使用),一样,在相同的全局正则表达式实例上多次调用<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><code class="language-javascript"><code class="language-js"><span class="keyword token">let</span> str <span class="operator token">=</span> <span class="string token">'hello world!'</span><span class="punctuation token">;</span>
|
||
<span class="keyword token">let</span> result <span class="operator token">=</span> <span class="regex token">/^hello/</span><span class="punctuation token">.</span><span class="function token">test</span><span class="punctuation token">(</span>str<span class="punctuation token">)</span><span class="punctuation token">;</span>
|
||
console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>result<span class="punctuation token">)</span><span class="punctuation token">;</span>
|
||
<span class="comment token">// true</span></code></code></pre>
|
||
<p>下例打印一条信息,该信息内容取决于是否成功通过指定测试:</p>
|
||
<pre><code class="language-javascript">function testinput(re, str){
|
||
var midstring;
|
||
if (re.test(str)) {
|
||
midstring = " contains ";
|
||
} else {
|
||
midstring = " does not contain ";
|
||
}
|
||
console.log(str + midstring + re.source);
|
||
}
|
||
</code></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 处开始匹配字符串,(<a href="Reference/Global_Objects/RegExp/exec" title="exec() 方法在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null。"><code><code>exec()</code></code></a> 同样改变正则本身的 <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><code>var regex = /foo/g;
|
||
|
||
// regex.lastIndex is at 0
|
||
regex.test('foo'); // true
|
||
|
||
// regex.lastIndex is now at 3
|
||
regex.test('foo'); // false</code></code></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><a class="external" href="https://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf" hreflang="en" lang="en" rel="noopener" title="ECMAScript 3rd Edition (ECMA-262)">ECMAScript 3rd Edition (ECMA-262)</a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>Initial definition. Implemented in JavaScript 1.2.</td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-15.10.6.3" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">RegExp.test</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-regexp.prototype.test" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">RegExp.test</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-regexp.prototype.test" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">RegExp.test</small></a></td>
|
||
<td><span class="spec-Draft">Draft</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p> </p>
|
||
<h3 id="sect1"> </h3>
|
||
<p> </p>
|
||
<h2 id="浏览器兼容性">浏览器兼容性</h2>
|
||
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
|
||
This compatibility table still uses the old format,
|
||
because we haven't yet converted the data it contains.
|
||
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
|
||
<div class="htab">
|
||
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
|
||
<ul>
|
||
<li class="selected"><a>Desktop</a></li>
|
||
<li><a>Mobile</a></li>
|
||
</ul>
|
||
</div><p></p>
|
||
<div id="compat-desktop">
|
||
<table class="compat-table">
|
||
<tbody>
|
||
<tr>
|
||
<th>Feature</th>
|
||
<th>Chrome</th>
|
||
<th>Firefox (Gecko)</th>
|
||
<th>Internet Explorer</th>
|
||
<th>Opera</th>
|
||
<th>Safari</th>
|
||
</tr>
|
||
<tr>
|
||
<td>Basic support</td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<div id="compat-mobile">
|
||
<table class="compat-table">
|
||
<tbody>
|
||
<tr>
|
||
<th>Feature</th>
|
||
<th>Android</th>
|
||
<th>Chrome for Android</th>
|
||
<th>Firefox Mobile (Gecko)</th>
|
||
<th>IE Mobile</th>
|
||
<th>Opera Mobile</th>
|
||
<th>Safari Mobile</th>
|
||
</tr>
|
||
<tr>
|
||
<td>Basic support</td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<h3 id="Gecko_特殊注意">Gecko 特殊注意</h3>
|
||
<p>在 Gecko 8.0之前的版本 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5), <code>test()</code> 被不正确地实现了;当无参数地调用时,它会匹配之前的输入值 (RegExp.input 属性),而不是字符串"undefined"。这已经被修正了;现在 <code>/undefined/.test()</code> 正确地返回<code>true</code>,而不是错误。</p>
|
||
<h2 id="相关链接">相关链接</h2>
|
||
<ul>
|
||
<li>在<a href="Guide" title="JavaScript/Guide">JavaScript指南</a>的<a href="Guide/Regular_Expressions" title="JavaScript/Guide/Regular_Expressions">正则表达式</a>章节</li>
|
||
<li><a href="Reference/RegExp" title="此页面仍未被本地化, 期待您的翻译!"><code>RegExp</code></a></li>
|
||
</ul>
|
||
</article> |