0.2.1 • Published 2 months ago
@stdlib/regexp-reviver v0.2.1
reviveRegExp
Revive a JSON-serialized regular expression.
Installation
npm install @stdlib/regexp-reviver
Usage
var reviveRegExp = require( '@stdlib/regexp-reviver' );
reviveRegExp( key, value )
Revives a JSON-serialized regular expression.
var parseJSON = require( '@stdlib/utils-parse-json' );
var str = '{"type":"RegExp","pattern":"ab+c","flags":""}';
var re = parseJSON( str, reviveRegExp );
// returns <RegExp>
For details on the JSON serialization format, see @stdlib/regexp-to-json
.
Examples
var parseJSON = require( '@stdlib/utils-parse-json' );
var regexp2json = require( '@stdlib/regexp-to-json' );
var reviveRegExp = require( '@stdlib/regexp-reviver' );
var re1 = /ab+c/;
var json = regexp2json( re1 );
var str = JSON.stringify( json );
// returns '{"type":"RegExp","pattern":"ab+c","flags":""}'
var re2 = parseJSON( str, reviveRegExp );
// returns <RegExp>
var bool = ( re1.toString() === re2.toString() );
// returns true
See Also
@stdlib/regexp-to-json
: return a JSON representation of a regular expression.
Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2024. The Stdlib Authors.