|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 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 | +# MatchSymbol |
| 22 | + |
| 23 | +> [symbol][mdn-symbol] which specifies whether an object should be treated as a regular expression. |
| 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 MatchSymbol = require( '@stdlib/symbol/match' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### MatchSymbol |
| 44 | + |
| 45 | +[`symbol`][mdn-symbol] which specifies whether an object should be treated as a regular expression. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var s = typeof MatchSymbol; |
| 49 | +// e.g., returns 'symbol' |
| 50 | +``` |
| 51 | + |
| 52 | +</section> |
| 53 | + |
| 54 | +<!-- /.usage --> |
| 55 | + |
| 56 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 57 | + |
| 58 | +<section class="notes"> |
| 59 | + |
| 60 | +## Notes |
| 61 | + |
| 62 | +- The [symbol][mdn-symbol] is only supported in environments which support [symbols][mdn-symbol]. In non-supporting environments, the value is `null`. |
| 63 | +- The `MatchSymbol` is used by `String.prototype.match` to match an input string against an object. When `String.prototype.match` is called, it checks whether the argument has a `MatchSymbol` method. If present, this method is invoked with the input string as its argument, and the returned value becomes the result of the match operation. |
| 64 | +- The presence of `MatchSymbol` on an object also determines whether the object should be treated as a regular expression when used with string matching methods. |
| 65 | + |
| 66 | +</section> |
| 67 | + |
| 68 | +<!-- /.notes --> |
| 69 | + |
| 70 | +<!-- Package usage examples. --> |
| 71 | + |
| 72 | +<section class="examples"> |
| 73 | + |
| 74 | +## Examples |
| 75 | + |
| 76 | +<!-- eslint no-undef: "error" --> |
| 77 | + |
| 78 | +```javascript |
| 79 | +var defineProperty = require( '@stdlib/utils/define-property' ); |
| 80 | +var MatchSymbol = require( '@stdlib/symbol/match' ); |
| 81 | + |
| 82 | +var obj = {}; |
| 83 | +var re = /foo/; |
| 84 | + |
| 85 | +// Define a custom match behavior: |
| 86 | +function customMatch() { |
| 87 | + return [ 'custom', 'match', 'result' ]; |
| 88 | +} |
| 89 | + |
| 90 | +// Attach the custom matcher using Symbol.match: |
| 91 | +defineProperty( obj, MatchSymbol, { |
| 92 | + 'configurable': false, |
| 93 | + 'enumerable': false, |
| 94 | + 'writable': false, |
| 95 | + 'value': customMatch |
| 96 | +}); |
| 97 | + |
| 98 | +// Using custom match behavior: |
| 99 | +var v = 'foobar'.match( obj ); |
| 100 | +console.log( v ); |
| 101 | +// => [ 'custom', 'match', 'result' ] |
| 102 | + |
| 103 | +// Default regular expression match: |
| 104 | +v = 'football'.match( re ); |
| 105 | +console.log( v ); |
| 106 | +// => [ 'foo' ] |
| 107 | +``` |
| 108 | + |
| 109 | +</section> |
| 110 | + |
| 111 | +<!-- /.examples --> |
| 112 | + |
| 113 | +<!-- 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. --> |
| 114 | + |
| 115 | +<section class="references"> |
| 116 | + |
| 117 | +</section> |
| 118 | + |
| 119 | +<!-- /.references --> |
| 120 | + |
| 121 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 122 | + |
| 123 | +<section class="related"> |
| 124 | + |
| 125 | +* * * |
| 126 | + |
| 127 | +## See Also |
| 128 | + |
| 129 | +- <span class="package-name">[`@stdlib/symbol/ctor`][@stdlib/symbol/ctor]</span><span class="delimiter">: </span><span class="description">symbols.</span> |
| 130 | + |
| 131 | +</section> |
| 132 | + |
| 133 | +<!-- /.related --> |
| 134 | + |
| 135 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 136 | + |
| 137 | +<section class="links"> |
| 138 | + |
| 139 | +[mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol |
| 140 | + |
| 141 | +<!-- <related-links> --> |
| 142 | + |
| 143 | +[@stdlib/symbol/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/symbol/ctor |
| 144 | + |
| 145 | +<!-- </related-links> --> |
| 146 | + |
| 147 | +</section> |
| 148 | + |
| 149 | +<!-- /.links --> |
0 commit comments