README.md (2216B)
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 # RegExp 22 23 > Create a [regular expression][regexp] from a [regular expression][regexp] string. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var reFromString = require( '@stdlib/utils/regexp-from-string' ); 31 ``` 32 33 #### reFromString( str ) 34 35 Parses a [regular expression][regexp] `string` and returns a new [regular expression][regexp]. 36 37 ```javascript 38 var re = reFromString( '/beep/' ); 39 // returns /beep/ 40 ``` 41 42 </section> 43 44 <!-- /.usage --> 45 46 <section class="notes"> 47 48 ## Notes 49 50 - Provided `strings` **must** be properly **escaped**. 51 52 <!-- eslint-disable no-useless-escape --> 53 54 ```javascript 55 // Unescaped: 56 var re = reFromString( '/\w+/' ); 57 // returns /w+/ 58 59 // Escaped: 60 re = reFromString( '/\\w+/' ); 61 // returns /\w+/ 62 ``` 63 64 </section> 65 66 <!-- /.notes --> 67 68 <section class="examples"> 69 70 ## Examples 71 72 <!-- eslint-disable no-useless-escape --> 73 74 <!-- eslint no-undef: "error" --> 75 76 ```javascript 77 var reFromString = require( '@stdlib/utils/regexp-from-string' ); 78 79 var re = reFromString( '/beep/' ); 80 // returns /beep/ 81 82 re = reFromString( '/[A-Z]*/' ); 83 // returns /[A-Z]*/ 84 85 re = reFromString( '/\\\\\\\//ig' ); 86 // returns /\\\//gi 87 88 re = reFromString( '/[A-Z]{0,}/' ); 89 // returns /[A-Z]{0,}/ 90 91 re = reFromString( '/^boop$/' ); 92 // returns /^boop$/ 93 94 re = reFromString( '/(?:.*)/' ); 95 // returns /(?:.*)/ 96 97 re = reFromString( '/(?:beep|boop)/' ); 98 // returns /(?:beep|boop)/ 99 100 re = reFromString( '/\\w+/' ); 101 // returns /\w+/ 102 ``` 103 104 </section> 105 106 <!-- /.examples --> 107 108 <section class="links"> 109 110 [regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 111 112 </section> 113 114 <!-- /.links -->