README.md (3440B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # EOL 22 23 > [Regular expression][mdn-regexp] to match a [newline][newline] character sequence. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var reEOL = require( '@stdlib/regexp/eol' ); 31 ``` 32 33 #### reEOL( \[options] ) 34 35 Returns a [Regular expression][mdn-regexp] to match a [newline][newline] character sequence. 36 37 ```javascript 38 var RE_EOL = reEOL(); 39 40 var bool = RE_EOL.test( '\n' ); 41 // returns true 42 43 bool = RE_EOL.test( '\r\n' ); 44 // returns true 45 46 bool = RE_EOL.test( '\\r\\n' ); 47 // returns false 48 ``` 49 50 The function accepts an `options` object with optional properties: 51 52 - **flags**: `string` specifying regular expression [flags][mdn-regexp-flags]. Default: `''`. 53 - **capture**: `boolean` indicating whether to create a capture group for the match. Default: `false`. 54 55 By default, the function returns a regular expression which does not have any flags specified. To specify [flags][mdn-regexp-flags], set the `flags` option with a list of flags (which may be in any order). 56 57 ```javascript 58 var replace = require( '@stdlib/string/replace' ); 59 60 var RE_EOL = reEOL({ 61 'flags': 'g' 62 }); 63 64 var str = '1\n2\n3'; 65 var out = replace( str, RE_EOL, '' ); 66 // returns '123' 67 ``` 68 69 By default, the function returns a regular expression which does not capture the part of a string matching the regular expression. To capture matches, set the `capture` option. 70 71 ```javascript 72 var RE_EOL = reEOL({ 73 'capture': true 74 }); 75 76 var str = 'beep\nboop'; 77 var arr = str.split( RE_EOL ); 78 // returns [ 'beep', '\n', 'boop' ] 79 ``` 80 81 #### reEOL.REGEXP 82 83 [Regular expression][mdn-regexp] to match a [newline][newline] character sequence. 84 85 ```javascript 86 var bool = reEOL.REGEXP.test( 'abc' ); 87 // returns false 88 ``` 89 90 #### reEOL.REGEXP_CAPTURE 91 92 [Regular expression][mdn-regexp] to capture a [newline][newline] character sequence. 93 94 ```javascript 95 var parts = reEOL.REGEXP_CAPTURE.exec( '\n' ); 96 // returns [ '\n', '\n' ] 97 ``` 98 99 </section> 100 101 <!-- /.usage --> 102 103 <section class="examples"> 104 105 ## Examples 106 107 <!-- eslint no-undef: "error" --> 108 109 ```javascript 110 var reEOL = require( '@stdlib/regexp/eol' ); 111 112 var RE_EOL = reEOL(); 113 var bool; 114 var str; 115 116 bool = RE_EOL.test( '\r\n' ); 117 // returns true 118 119 bool = RE_EOL.test( '\n' ); 120 // returns true 121 122 bool = RE_EOL.test( '\r' ); 123 // returns false 124 125 bool = RE_EOL.test( '\\r\\n' ); 126 // returns false 127 128 bool = RE_EOL.test( 'beep' ); 129 // returns false 130 131 str = 'This is\na newline\r\ndelimited string.'; 132 133 var arr = str.split( RE_EOL ); 134 // returns [ 'This is', 'a newline', 'delimited string.' ] 135 ``` 136 137 </section> 138 139 <!-- /.examples --> 140 141 <section class="links"> 142 143 [mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 144 145 [mdn-regexp-flags]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#advanced_searching_with_flags_2 146 147 [newline]: https://en.wikipedia.org/wiki/Newline 148 149 </section> 150 151 <!-- /.links -->