Skip to content

Commit 0c1e734

Browse files
committed
matchRecursive: improve documentation
1 parent 6e1711e commit 0c1e734

File tree

3 files changed

+39
-42
lines changed

3 files changed

+39
-42
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ XRegExp.tag()`${backref1}${backref2}`.test('aabb'); // -> true
186186

187187
### XRegExp.matchRecursive
188188

189-
Match recursive constructs using XRegExp pattern strings as left and right delimiters:
189+
A robust and flexible API for matching recursive constructs using XRegExp pattern strings as left and right delimiters:
190190

191191
```js
192192
const str1 = '(t((e))s)t()(ing)';
@@ -232,7 +232,7 @@ XRegExp.matchRecursive(str5, '<div\\s*>', '</div>', 'gi', {
232232
// -> ['an']
233233
```
234234

235-
`XRegExp.matchRecursive` throws an error if it scans past an unbalanced delimiter in the target string.
235+
By default, `XRegExp.matchRecursive` throws an error if it scans past an unbalanced delimiter in the target string. Multiple alternative options are available for handling unbalanced delimiters.
236236

237237
## Installation and usage
238238

src/addons/matchrecursive.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,33 @@ export default (XRegExp) => {
2222

2323
/**
2424
* Returns an array of match strings between outermost left and right delimiters, or an array of
25-
* objects with detailed match parts and position data. An error is thrown if delimiters are
26-
* unbalanced within the data.
25+
* objects with detailed match parts and position data. By default, an error is thrown if
26+
* delimiters are unbalanced within the subject string.
2727
*
2828
* @memberOf XRegExp
2929
* @param {String} str String to search.
3030
* @param {String} left Left delimiter as an XRegExp pattern.
3131
* @param {String} right Right delimiter as an XRegExp pattern.
3232
* @param {String} [flags] Any combination of XRegExp flags, used for the left and right delimiters.
3333
* @param {Object} [options] Options object with optional properties:
34-
* - `valueNames` {Array} Providing `valueNames` changes the overall return value from a
35-
* simple array of matched strings to an array of objects that provide greatly extended
36-
* information including value and position information about not only the matched strings
37-
* but also the matched delimiters and the strings outside of or between matches.
38-
* To use this extended information mode, provide 4 strings to name the parts that will be
39-
* returned: 1. values outside of (before, after, and between) matches, 2. the matched outer
40-
* left delimiter, 3. the matched text between outer left and right delimiters, and 4. the
41-
* matched outer right delimiter. Null values can be provided instead of strings for any of
42-
* these 4 parts to omit unneeded parts from the returned results.
34+
* - `valueNames` {Array} Providing `valueNames` changes the return value from an array of
35+
* matched strings to an array of objects that provide the value and start/end positions
36+
* for the matched strings as well as the matched delimiters and unmatched string segments.
37+
* To use this extended information mode, provide an array of 4 strings that name the parts
38+
* to be returned:
39+
* 1. String segments outside of (before, between, and after) matches.
40+
* 2. Matched outermost left delimiters.
41+
* 3. Matched text between the outermost left and right delimiters.
42+
* 4. Matched outermost right delimiters.
43+
* Taken together, these parts include the entire subject string if used with flag g.
44+
* Use `null` for any of these values to omit unneeded parts from the returned results.
4345
* - `escapeChar` {String} Single char used to escape delimiters within the subject string.
44-
* - `unbalanced` {String} How to handle unbalanced delimiters within the subject string.
45-
* Valid values are:
46+
* - `unbalanced` {String} Handling mode for unbalanced delimiters. Options are:
4647
* - 'error' - throw (default)
47-
* - 'skip' - treat unbalanced delimiters as part of the text between delimiters, and
48-
* continue searching after the unbalanced delimiter.
49-
* - 'skip-lazy' - treat unbalanced delimiters as part of the text between delimiters,
50-
* and continue searching one character after the start of the unbalanced delimiter.
48+
* - 'skip' - unbalanced delimiters are treated as part of the text between delimiters, and
49+
* searches continue at the end of the unbalanced delimiter.
50+
* - 'skip-lazy' - unbalanced delimiters are treated as part of the text between delimiters,
51+
* and searches continue one character after the start of the unbalanced delimiter.
5152
* @returns {Array} Array of matches, or an empty array.
5253
* @example
5354
*

types/index.d.ts

+19-23
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,7 @@ declare namespace XRegExp {
340340
valueNames?: MatchRecursiveValueNames | null;
341341

342342
/**
343-
* How to handle unbalanced delimiters within the subject string. Valid values are:
344-
* - 'error' - throw (default).
345-
* - 'skip' - treat unbalanced delimiters as part of the text between delimiters, and
346-
* continue searching after the unbalanced delimiter.
347-
* - 'skip-lazy' - treat unbalanced delimiters as part of the text between delimiters,
348-
* and continue searching one character after the start of the unbalanced delimiter.
343+
* Handling mode for unbalanced delimiters.
349344
*/
350345
unbalanced?: 'error' | 'skip' | 'skip-lazy';
351346
}
@@ -755,31 +750,32 @@ declare namespace XRegExp {
755750

756751
/**
757752
* Returns an array of match strings between outermost left and right delimiters, or an array of
758-
* objects with detailed match parts and position data. An error is thrown if delimiters are
759-
* unbalanced within the data.
753+
* objects with detailed match parts and position data. By default, an error is thrown if
754+
* delimiters are unbalanced within the subject string.
760755
*
761756
* @param str - String to search.
762757
* @param left - Left delimiter as an XRegExp pattern.
763758
* @param right - Right delimiter as an XRegExp pattern.
764759
* @param flags - Any combination of XRegExp flags, used for the left and right delimiters.
765760
* @param options - Options object with optional properties:
766-
* - `valueNames` {Array} Providing `valueNames` changes the overall return value from a
767-
* simple array of matched strings to an array of objects that provide greatly extended
768-
* information including value and position information about not only the matched strings
769-
* but also the matched delimiters and the strings outside of or between matches.
770-
* To use this extended information mode, provide 4 strings to name the parts that will be
771-
* returned: 1. values outside of (before, after, and between) matches, 2. the matched outer
772-
* left delimiter, 3. the matched text between outer left and right delimiters, and 4. the
773-
* matched outer right delimiter. Null values can be provided instead of strings for any of
774-
* these 4 parts to omit unneeded parts from the returned results.
761+
* - `valueNames` {Array} Providing `valueNames` changes the return value from an array of
762+
* matched strings to an array of objects that provide the value and start/end positions
763+
* for the matched strings as well as the matched delimiters and unmatched string segments.
764+
* To use this extended information mode, provide an array of 4 strings that name the parts
765+
* to be returned:
766+
* 1. String segments outside of (before, between, and after) matches.
767+
* 2. Matched outermost left delimiters.
768+
* 3. Matched text between the outermost left and right delimiters.
769+
* 4. Matched outermost right delimiters.
770+
* Taken together, these parts include the entire subject string if used with flag g.
771+
* Use `null` for any of these values to omit unneeded parts from the returned results.
775772
* - `escapeChar` {String} Single char used to escape delimiters within the subject string.
776-
* - `unbalanced` {String} How to handle unbalanced delimiters within the subject string.
777-
* Valid values are:
773+
* - `unbalanced` {String} Handling mode for unbalanced delimiters. Options are:
778774
* - 'error' - throw (default)
779-
* - 'skip' - treat unbalanced delimiters as part of the text between delimiters, and
780-
* continue searching after the unbalanced delimiter.
781-
* - 'skip-lazy' - treat unbalanced delimiters as part of the text between delimiters,
782-
* and continue searching one character after the start of the unbalanced delimiter.
775+
* - 'skip' - unbalanced delimiters are treated as part of the text between delimiters, and
776+
* searches continue at the end of the unbalanced delimiter.
777+
* - 'skip-lazy' - unbalanced delimiters are treated as part of the text between delimiters,
778+
* and searches continue one character after the start of the unbalanced delimiter.
783779
* @returns Array of matches, or an empty array.
784780
* @example
785781
*

0 commit comments

Comments
 (0)