@stdlib/regexp
Version:
Regular expressions.
141 lines (88 loc) • 3.15 kB
Markdown
<!--
@license Apache-2.0
Copyright (c) 2022 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
# regexp2json
> Return a [JSON][json] representation of a [regular expression][mdn-regexp].
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
<section class="intro">
</section>
<!-- /.intro -->
<!-- Package usage documentation. -->
<section class="usage">
## Usage
```javascript
var regexp2json = require( '@stdlib/regexp/to-json' );
```
#### regexp2json( regexp )
Returns a [JSON][json] representation of a [regular expression][mdn-regexp].
```javascript
var json = regexp2json( /ab+c/ );
/* returns
{
'type': 'RegExp',
'pattern': 'ab+c',
'flags': ''
}
*/
```
The returned object has the following properties:
- **type**: value type. The assigned value is always `'RegExp'`.
- **pattern**: regular expression pattern.
- **flags**: regular expression flags.
</section>
<!-- /.usage -->
<!-- Package usage examples. -->
<section class="examples">
## Examples
<!-- eslint no-undef: "error" -->
```javascript
var regexp2json = require( '@stdlib/regexp/to-json' );
var out = regexp2json( /.*/ );
/* returns
{
'type': 'RegExp',
'pattern': '.*',
'flags': ''
}
*/
out = regexp2json( /ab+c/g );
/* returns
{
'type': 'RegExp',
'pattern': 'ab+c',
'flags': 'g'
}
*/
```
</section>
<!-- /.examples -->
<!-- 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. -->
<section class="references">
</section>
<!-- /.references -->
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
<section class="related">
* * *
## See Also
- <span class="package-name">[`@stdlib/regexp/reviver`][@stdlib/regexp/reviver]</span><span class="delimiter">: </span><span class="description">revive a JSON-serialized regular expression.</span>
</section>
<!-- /.related -->
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="links">
[json]: http://www.json.org/
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
<!-- <related-links> -->
[@stdlib/regexp/reviver]: https://github.com/stdlib-js/regexp/tree/main/reviver
<!-- </related-links> -->
</section>
<!-- /.links -->