aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/conflicting/web/javascript/guide/regular_expressions_24eb6498b86da57e7fb5337fd8fa04a6/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-tw/conflicting/web/javascript/guide/regular_expressions_24eb6498b86da57e7fb5337fd8fa04a6/index.html')
-rw-r--r--files/zh-tw/conflicting/web/javascript/guide/regular_expressions_24eb6498b86da57e7fb5337fd8fa04a6/index.html46
1 files changed, 46 insertions, 0 deletions
diff --git a/files/zh-tw/conflicting/web/javascript/guide/regular_expressions_24eb6498b86da57e7fb5337fd8fa04a6/index.html b/files/zh-tw/conflicting/web/javascript/guide/regular_expressions_24eb6498b86da57e7fb5337fd8fa04a6/index.html
new file mode 100644
index 0000000000..b8317583d4
--- /dev/null
+++ b/files/zh-tw/conflicting/web/javascript/guide/regular_expressions_24eb6498b86da57e7fb5337fd8fa04a6/index.html
@@ -0,0 +1,46 @@
+---
+title: 括弧子字串的比對結果的運用
+slug: >-
+ conflicting/Web/JavaScript/Guide/Regular_Expressions_24eb6498b86da57e7fb5337fd8fa04a6
+translation_of: Web/JavaScript/Guide/Regular_Expressions#Using_Parenthesized_Substring_Matches
+translation_of_original: >-
+ Web/JavaScript/Guide/Obsolete_Pages/Working_with_Regular_Expressions/Using_Parenthesized_Substring_Matches
+original_slug: >-
+ Web/JavaScript/Obsolete_Pages/Obsolete_Pages/Obsolete_Pages/正規表達式的運用/括弧子字串的比對結果的運用
+---
+<h4 id="括弧子字串的比對結果的運用" name="括弧子字串的比對結果的運用">括弧子字串的比對結果的運用</h4>
+<p>在正規表達式的模式中包含括弧,對應的子比對結果就會被記憶。舉例來說,<code>/a(b)c/</code> 比對字元 'abc' 並把 'b' 記憶起來。若要取回括弧子字串的比對結果,就使用 Array 元素 {{ mediawiki.external(1) }}, ..., {{ mediawiki.external('n') }}。</p>
+<p>括弧子字串的可能數目並沒有限制。返回的陣列保留了所有找到的子字串。以下範例解說如何使用括弧子字串的比對結果。</p>
+<p><strong>範例 1</strong><br>
+以下 Script 使用 <code>replace</code> 方法來置換字串裡的文字。對於那些替換用的文字,Script 使用了 <code>$1</code> 和 <code>$2</code> 來表示第一個和第二個括弧子字串的比對結果。</p>
+<pre>&lt;script type="text/javascript"&gt;
+ re = /(\w+)\s(\w+)/;
+ str = "John Smith";
+ newstr = str.replace(re, "$2, $1");
+ document.write(newstr);
+&lt;/script&gt;
+</pre>
+<p>本例輸出 "Smith, John"。</p>
+<p><strong>範例 2</strong><br>
+附註: 在 <code>getInfo</code> 函數中,<code>exec</code> 方法是以 () 省略記法所呼叫的,這個記法在 Firefox 可以運作,其他瀏覽器則不一定。</p>
+<pre>&lt;html&gt;
+
+&lt;script type="text/javascript"&gt;
+ function getInfo(field){
+ var a = /(\w+)\s(\d+)/(field.value);
+ window.alert(a[1] + ", your age is " + a[2]);
+ }
+&lt;/script&gt;
+
+Enter your first name and your age, and then press Enter.
+
+&lt;form&gt;
+ &lt;input type="text" name="NameAge" onchange="getInfo(this);"&gt;
+&lt;/form&gt;
+
+&lt;/html&gt;
+</pre>
+<div class="noinclude">
+<p>{{ PreviousNext("Core_JavaScript_1.5_教學:正規表達式的運用", "Core_JavaScript_1.5_教學:正規表達式的運用:使用標誌的進階搜尋") }}</p>
+</div>
+<p>{{ languages( { "en": "en/Core_JavaScript_1.5_Guide/Working_with_Regular_Expressions/Using_Parenthesized_Substring_Matches", "es": "es/Gu\u00eda_JavaScript_1.5/Trabajar_con_expresiones_regulares/Usar_coincidencias_de_subcadenas_parentizadas", "fr": "fr/Guide_JavaScript_1.5/Travailler_avec_les_expressions_rationnelles/Utilisation_des_parenth\u00e8ses_de_capture", "ja": "ja/Core_JavaScript_1.5_Guide/Working_with_Regular_Expressions/Using_Parenthesized_Substring_Matches", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Praca_z_wyra\u017ceniami_regularnymi/U\u017cycie_odpowiedniego_znaku" } ) }}</p>