<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 問題 Problem 你想搜索一個字符串,獲取該子字符串匹配的起始位置或匹配值。 You need to search for a substring, and return either the starting position of the match or the matching value itself. ## 方法 Solution 使用正則表達式有好幾種方法來實現,有些是調用`RegExp`字面量或者對象的方法,有的是調用`String`對象上的。 There are several ways to accomplish this using regular expressions. Some methods are called on a?`RegExp`?pattern or object and some are called on?`String`?objects. ### `RegExp`對象?`RegExp`?objects 第一種方法是調用`RegExp`字面量或者對象上的`test`方法,這個`test`方法會返回一個布爾值: The first way is to call the?`test`?method on a`RegExp`?pattern or object. The?`test`?method returns a boolean value: ~~~ match = /sample/.test("Sample text") # => false match = /sample/i.test("Sample text") # => true ~~~ 下一種方法是調用`RegExp`字面量或者對象的`exec`方法,這個方法會返回一個數組(包含了匹配信息)或者`null`; The next way to is to call the?`exec`?method on a?`RegExp`?pattern or object. The?`exec`?method returns an array an array with the match information or?`null`: ~~~ match = /s(amp)le/i.exec "Sample text" # => [ 'Sample', 'amp', index: 0, input: 'Sample text' ] match = /s(amp)le/.exec "Sample text" # => null ~~~ ### `String`對象?`String`?objects `match`方法對一個給定的字符串與`RegExp`匹配。如果帶有`g`標志,則返回一個包含了所有匹配的數組,否則只返回第一個匹配值,或者返回`null`,如果沒有匹配的話。 The?`match`?method matches a given string with the?`RegExp`. With ‘g’ flag returns an array containing the matches, without ‘g’ flag returns just the first match or if no match is found returns?`null`. ~~~ "Watch out for the rock!".match(/r?or?/g) # => [ 'o', 'or', 'ro' ] "Watch out for the rock!".match(/r?or?/) # => [ 'o', index: 6, input: 'Watch out for the rock!' ] "Watch out for the rock!".match(/ror/) # => null ~~~ `search`用來做`RegExp`和字符串的匹配,如果匹配到就返回匹配的起始位置,否則返回-1。 The?`search`?method matches?`RegExp`?with string and returns the index of the beginning of the match if found, -1 if not. ~~~ "Watch out for the rock!".search /for/ # => 10 "Watch out for the rock!".search /rof/ # => -1 ~~~ ## 討論 Discussion 正則表達式是一種測試或者匹配字串的強大的方式。 Regular Expressions are a powerful way to test and match substrings.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看