mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-18 08:54:32 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
<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)</pre>
|
||||
<pre><var>regexObj</var>.test(str)</code></pre>
|
||||
<h3 id="Parameters" name="Parameters">参数</h3>
|
||||
<dl>
|
||||
<dt><code>str</code></dt>
|
||||
@@ -15,12 +15,12 @@
|
||||
<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 line-numbers language-js"><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>
|
||||
<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></pre>
|
||||
<span class="comment token">// true</span></code></code></pre>
|
||||
<p>下例打印一条信息,该信息内容取决于是否成功通过指定测试:</p>
|
||||
<pre class="brush: js">function testinput(re, str){
|
||||
<pre><code class="language-javascript">function testinput(re, str){
|
||||
var midstring;
|
||||
if (re.test(str)) {
|
||||
midstring = " contains ";
|
||||
@@ -29,7 +29,7 @@ console<span class="punctuation token">.</span><span class="function token">log<
|
||||
}
|
||||
console.log(str + midstring + re.source);
|
||||
}
|
||||
</pre>
|
||||
</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>
|
||||
@@ -39,7 +39,7 @@ console<span class="punctuation token">.</span><span class="function token">log<
|
||||
regex.test('foo'); // true
|
||||
|
||||
// regex.lastIndex is now at 3
|
||||
regex.test('foo'); // false</code></pre>
|
||||
regex.test('foo'); // false</code></code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
Reference in New Issue
Block a user