|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2022 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 | +# reviveRegExp |
| 22 | + |
| 23 | +> Revive a JSON-serialized [regular expression][regexp]. |
| 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 reviveRegExp = require( '@stdlib/regexp/reviver' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### reviveRegExp( key, value ) |
| 44 | + |
| 45 | +Revives a JSON-serialized [regular expression][regexp]. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var str = '{"type":"RegExp","pattern":"ab+c","flags":""}'; |
| 49 | + |
| 50 | +var regex = JSON.parse( str, reviveRegExp ); |
| 51 | +// returns <RegExp> |
| 52 | +``` |
| 53 | + |
| 54 | +For details on the JSON serialization format, see [regexp-to-json][@stdlib/regexp/to-json]. |
| 55 | + |
| 56 | +</section> |
| 57 | + |
| 58 | +<!-- /.usage --> |
| 59 | + |
| 60 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 61 | + |
| 62 | +<section class="examples"> |
| 63 | + |
| 64 | +## Examples |
| 65 | + |
| 66 | +```javascript |
| 67 | +var regex2json = require( '@stdlib/regexp/to-json' ); |
| 68 | +var reviveRegExp = require( '@stdlib/regexp/reviver' ); |
| 69 | + |
| 70 | +var regex = /ab+c/; |
| 71 | +var json = regex2json( regex ); |
| 72 | +/* returns |
| 73 | + { |
| 74 | + 'type': 'RegExp', |
| 75 | + 'pattern': 'ab+c', |
| 76 | + 'flags': '' |
| 77 | + } |
| 78 | +*/ |
| 79 | + |
| 80 | +var str = JSON.stringify( json ); |
| 81 | +// returns '{"type":"RegExp","pattern":"ab+c","flags":""}' |
| 82 | + |
| 83 | +var regex2 = JSON.parse( str, reviveRegExp ); |
| 84 | +// returns <RegExp> |
| 85 | + |
| 86 | +var bool = ( String( regex ) === String( regex2 ) ); |
| 87 | +// returns true |
| 88 | +``` |
| 89 | + |
| 90 | +</section> |
| 91 | + |
| 92 | +<!-- /.examples --> |
| 93 | + |
| 94 | +<!-- 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. --> |
| 95 | + |
| 96 | +<section class="references"> |
| 97 | + |
| 98 | +</section> |
| 99 | + |
| 100 | +<!-- /.references --> |
| 101 | + |
| 102 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 103 | + |
| 104 | +<section class="related"> |
| 105 | + |
| 106 | +</section> |
| 107 | + |
| 108 | +<!-- /.related --> |
| 109 | + |
| 110 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 111 | + |
| 112 | +<section class="links"> |
| 113 | + |
| 114 | +<!-- <related-links> --> |
| 115 | + |
| 116 | +[regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions |
| 117 | + |
| 118 | +[@stdlib/regexp/to-json]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/regexp/to-json |
| 119 | + |
| 120 | +<!-- </related-links> --> |
| 121 | + |
| 122 | +</section> |
| 123 | + |
| 124 | +<!-- /.links --> |
0 commit comments