README.md (4620B)
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 # White Space 22 23 > [Regular expression][regexp] to match a [white space][whitespace] character. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var reWhitespace = require( '@stdlib/regexp/whitespace' ); 31 ``` 32 33 #### reWhitespace( \[options] ) 34 35 Returns a [regular expression][regexp] to match a [white space][whitespace] character. 36 37 ```javascript 38 var RE_WHITESPACE = reWhitespace(); 39 40 var bool = RE_WHITESPACE.test( '\n' ); 41 // returns true 42 43 bool = RE_WHITESPACE.test( ' ' ); 44 // returns true 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 RE_WHITESPACE = reWhitespace({ 56 'flags': 'gm' 57 }); 58 59 var str = 'Hello World!'; 60 var bool = RE_WHITESPACE.test( str ); 61 // returns true 62 63 bool = RE_WHITESPACE.test( str ); 64 // returns false 65 ``` 66 67 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. 68 69 ```javascript 70 var RE_WHITESPACE = reWhitespace({ 71 'capture': true 72 }); 73 var str = 'Hello World!'; 74 var arr = str.split( RE_WHITESPACE ); 75 // returns [ 'Hello', ' ', 'World!' ] 76 ``` 77 78 #### reWhitespace.REGEXP 79 80 [Regular expression][regexp] to match a [white space][whitespace] character. 81 82 ```javascript 83 var bool = reWhitespace.REGEXP.test( '\n' ); 84 // returns true 85 86 bool = reWhitespace.REGEXP.test( '\\n' ); 87 // returns false 88 ``` 89 90 #### reWhitespace.REGEXP_CAPTURE 91 92 [Regular expression][regexp] to capture characters matching a [white space][whitespace] character. 93 94 ```javascript 95 var replace = require( '@stdlib/string/replace' ); 96 97 var str = 'Duplicate capture'; 98 var out = replace( str, reWhitespace.REGEXP_CAPTURE, '$1$1' ); 99 // returns 'Duplicate capture' 100 ``` 101 102 </section> 103 104 <!-- /.usage --> 105 106 <section class="notes"> 107 108 ## Notes 109 110 - Matches the `25` characters defined as white space ("WSpace=Y","WS") characters in the Unicode `9.0` character database. 111 112 - Matches one related white space character without the Unicode character property "WSpace=Y" (zero width non-breaking space which was deprecated as of Unicode 3.2). 113 114 - The `REGEXP` regular expression is defined as 115 116 ```text 117 /[\u0009\u000A\u000B\u000C\u000D\u0020\u0085\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]/ 118 ``` 119 120 - The `REGEXP_CAPTURE` regular expression is defined as 121 122 ```text 123 /([\u0009\u000A\u000B\u000C\u000D\u0020\u0085\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF])/ 124 ``` 125 126 </section> 127 128 <!-- /.notes --> 129 130 <section class="examples"> 131 132 ## Examples 133 134 <!-- eslint no-undef: "error", stdlib/doctest: "off" --> 135 136 ```javascript 137 var reWhitespace = require( '@stdlib/regexp/whitespace' ); 138 139 var RE_WHITESPACE = reWhitespace(); 140 141 var bool = RE_WHITESPACE.test( 'beep boop' ); 142 // returns true 143 144 bool = RE_WHITESPACE.test( '\n' ); 145 // returns true 146 147 bool = RE_WHITESPACE.test( '\r' ); 148 // returns true 149 150 bool = RE_WHITESPACE.test( '\t' ); 151 // returns true 152 153 bool = RE_WHITESPACE.test( 'beep' ); 154 // returns false 155 156 var str = 'This is\na newline\r\ndelimited string.'; 157 158 var arr = str.split( RE_WHITESPACE ); 159 // returns [ 'This', 'is', 'a', 'newline', '', 'delimited', 'string.' ] 160 ``` 161 162 </section> 163 164 <!-- /.examples --> 165 166 <section class="links"> 167 168 [regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 169 170 [whitespace]: https://en.wikipedia.org/wiki/Whitespace_character 171 172 [mdn-regexp-flags]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#advanced_searching_with_flags_2 173 174 </section> 175 176 <!-- /.links -->