README.md (5094B)
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 # Decimal Number 22 23 > [Regular expression][mdn-regexp] to match a decimal number. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var reDecimalNumber = require( '@stdlib/regexp/decimal-number' ); 31 ``` 32 33 #### reDecimalNumber( \[options] ) 34 35 Returns a [regular expression][mdn-regexp] to match a decimal number. 36 37 ```javascript 38 var RE_DECIMAL_NUMBER = reDecimalNumber(); 39 40 var out = RE_DECIMAL_NUMBER.test( 'foo 1.234.' ); 41 // returns true 42 43 out = RE_DECIMAL_NUMBER.test( '2:3' ); 44 // returns false 45 ``` 46 47 The function accepts an `options` object with optional properties: 48 49 - **flags**: `string` specifying regular expression [flags][mdn-regexp-flags]. Default: `''`. 50 - **capture**: `boolean` indicating whether to create a capture group for the match. Default: `false`. 51 52 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). 53 54 ```javascript 55 var replace = require( '@stdlib/string/replace' ); 56 57 var RE_DECIMAL_NUMBER = reDecimalNumber({ 58 'flags': 'g' 59 }); 60 61 var str = 'beep 1.234 boop 1.234'; 62 var out = replace( str, RE_DECIMAL_NUMBER, '' ); 63 // returns 'beep boop ' 64 ``` 65 66 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. 67 68 ```javascript 69 var RE_DECIMAL_NUMBER = reDecimalNumber({ 70 'capture': true 71 }); 72 73 var out = RE_DECIMAL_NUMBER.exec( 'beep 1.234 boop' ).slice(); 74 // returns [ '1.234', '1.234' ] 75 76 out = RE_DECIMAL_NUMBER.exec( '' ); 77 // returns null 78 ``` 79 80 #### reDecimalNumber.REGEXP 81 82 [Regular expression][mdn-regexp] to match a decimal number. 83 84 ```javascript 85 var bool = reDecimalNumber.REGEXP.test( '2:3' ); 86 // returns false 87 ``` 88 89 #### reDecimalNumber.REGEXP_CAPTURE 90 91 [Regular expression][mdn-regexp] to capture characters matching a decimal number. 92 93 ```javascript 94 var parts = reDecimalNumber.REGEXP_CAPTURE.exec( '1.234' ); 95 // returns [ '1.234', '1.234' ] 96 ``` 97 98 </section> 99 100 <!-- /.usage --> 101 102 <section class="notes"> 103 104 ## Notes 105 106 - A leading digit is not required. 107 108 ```javascript 109 var bool = reDecimalNumber.REGEXP.test( '.5' ); 110 // returns true 111 ``` 112 113 - A decimal point and at least one trailing digit is required. 114 115 ```javascript 116 var bool = reDecimalNumber.REGEXP.test( '5.' ); 117 // returns false 118 ``` 119 120 - The `REGEXP` regular expression is defined as 121 122 ```text 123 /[-+]{0,1}[0-9]*\.[0-9]+/ 124 ``` 125 126 - The `REGEXP_CAPTURE` regular expression is defined as 127 128 ```text 129 /([-+]{0,1}[0-9]*\.[0-9]+)/ 130 ``` 131 132 </section> 133 134 <!-- /.notes --> 135 136 <section class="examples"> 137 138 ## Examples 139 140 <!-- eslint no-undef: "error" --> 141 142 ```javascript 143 var reDecimalNumber = require( '@stdlib/regexp/decimal-number' ); 144 145 var RE_DECIMAL_NUMBER = reDecimalNumber(); 146 147 var bool = RE_DECIMAL_NUMBER.test( '1.234' ); 148 // returns true 149 150 bool = RE_DECIMAL_NUMBER.test( 'beep 1.234' ); 151 // returns true 152 153 bool = RE_DECIMAL_NUMBER.test( '1.234 boop' ); 154 // returns true 155 156 bool = RE_DECIMAL_NUMBER.test( 'foo 1.234.' ); 157 // returns true 158 159 bool = RE_DECIMAL_NUMBER.test( 'foo 1.234.567.890' ); 160 // returns true 161 162 bool = RE_DECIMAL_NUMBER.test( '1.234!' ); 163 // returns true 164 165 bool = RE_DECIMAL_NUMBER.test( '0.234' ); 166 // returns true 167 168 bool = RE_DECIMAL_NUMBER.test( '.234' ); 169 // returns true 170 171 bool = RE_DECIMAL_NUMBER.test( 'beep .234' ); 172 // returns true 173 174 bool = RE_DECIMAL_NUMBER.test( '.234 boop' ); 175 // returns true 176 177 bool = RE_DECIMAL_NUMBER.test( '1.0' ); 178 // returns true 179 180 bool = RE_DECIMAL_NUMBER.test( '-1.0' ); 181 // returns true 182 183 bool = RE_DECIMAL_NUMBER.test( '+1.0' ); 184 // returns true 185 186 bool = RE_DECIMAL_NUMBER.test( '0.0' ); 187 // returns true 188 189 bool = RE_DECIMAL_NUMBER.test( '.0' ); 190 // returns true 191 192 bool = RE_DECIMAL_NUMBER.test( '1.234:' ); 193 // returns true 194 195 bool = RE_DECIMAL_NUMBER.test( '1.234%' ); 196 // returns true 197 198 bool = RE_DECIMAL_NUMBER.test( '0' ); 199 // returns false 200 201 bool = RE_DECIMAL_NUMBER.test( 'beep 0' ); 202 // returns false 203 204 bool = RE_DECIMAL_NUMBER.test( '2:3' ); 205 // returns false 206 207 bool = RE_DECIMAL_NUMBER.test( 'beep' ); 208 // returns false 209 210 bool = RE_DECIMAL_NUMBER.test( '' ); 211 // returns false 212 ``` 213 214 </section> 215 216 <!-- /.examples --> 217 218 <section class="links"> 219 220 [mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 221 222 [mdn-regexp-flags]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#advanced_searching_with_flags_2 223 224 </section> 225 226 <!-- /.links -->