README.md (3968B)
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 # rescape 22 23 > Escape a [regular expression][mdn-regexp] string or pattern. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var rescape = require( '@stdlib/utils/escape-regexp-string' ); 41 ``` 42 43 #### rescape( str ) 44 45 Escapes a [regular expression][mdn-regexp] `string` or pattern. 46 47 ```javascript 48 var str = rescape( '/[A-Z]*/' ); 49 // returns '/\\[A\\-Z\\]\\*/' 50 51 str = rescape( '[A-Z]*' ); 52 // returns '\\[A\\-Z\\]\\*' 53 ``` 54 55 If provided a value which is not a primitive `string`, the function **throws** a `TypeError`. 56 57 ```javascript 58 try { 59 rescape( null ); 60 // throws an error... 61 } catch ( err ) { 62 console.error( err ); 63 } 64 ``` 65 66 </section> 67 68 <!-- /.usage --> 69 70 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 71 72 <section class="notes"> 73 74 - The following characters have special meaning inside of regular expressions and need to be escaped in case the characters should be treated literally: 75 76 | description | value | 77 | ------------- | -------- | 78 | Backslash | `\` | 79 | Braces | `{ }` | 80 | Brackets | `[ ]` | 81 | Caret | `^` | 82 | Dollar Sign | `$` | 83 | Forward Slash | `/` | 84 | Asterisk | `*` | 85 | Parentheses | `( )` | 86 | Period | `.` | 87 | Plus Sign | `+` | 88 | Vertical Bar | `|` | 89 | Question Mark | `?` | 90 91 </section> 92 93 <!-- /.notes --> 94 95 <!-- Package usage examples. --> 96 97 <section class="examples"> 98 99 ## Examples 100 101 <!-- eslint-disable no-useless-escape --> 102 103 <!-- eslint no-undef: "error" --> 104 105 ```javascript 106 var rescape = require( '@stdlib/utils/escape-regexp-string' ); 107 108 var out = rescape( '/beep/' ); 109 // returns '/beep/' 110 111 out = rescape( 'beep' ); 112 // returns 'beep' 113 114 out = rescape( '/[A-Z]*/' ); 115 // returns '/\\[A\\-Z\\]\\*/' 116 117 out = rescape( '[A-Z]*' ); 118 // returns '\\[A\\-Z\\]\\*' 119 120 out = rescape( '/\\\//ig' ); 121 // returns '/\\\\\\\//ig' 122 123 out = rescape( '\\\/' ); 124 // returns '\\\\\\\/' 125 126 out = rescape( '/[A-Z]{0,}/' ); 127 // returns '/\\[A\\-Z\\]\\{0,\\}/' 128 129 out = rescape( '[A-Z]{0,}' ); 130 // returns '\\[A\\-Z\\]\\{0,\\}' 131 132 out = rescape( '/^boop$/' ); 133 // returns '/\\^boop\\$/' 134 135 out = rescape( '^boop$' ); 136 // returns '\\^boop\\$' 137 138 out = rescape( '/(?:.*)/' ); 139 // returns '/\\(\\?:\\.\\*\\)/' 140 141 out = rescape( '(?:.*)' ); 142 // returns '\\(\\?:\\.\\*\\)' 143 144 out = rescape( '/(?:beep|boop)/' ); 145 // returns '/\\(\\?:beep\\|boop\\)/' 146 147 out = rescape( '(?:beep|boop)' ); 148 // returns '\\(\\?:beep\\|boop\\)' 149 ``` 150 151 </section> 152 153 <!-- /.examples --> 154 155 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 156 157 <section class="references"> 158 159 </section> 160 161 <!-- /.references --> 162 163 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 164 165 <section class="links"> 166 167 [mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 168 169 </section> 170 171 <!-- /.links -->