@@ -754,8 +754,8 @@ <h2 id="matchRecursive"><code>XRegExp.matchRecursive(<span class="plain">str, le
754
754
< p > < strong > Requires the XRegExp.matchRecursive addon</ strong > , which is bundled in < code > xregexp-all.js</ code > .</ p >
755
755
756
756
< p > Returns an array of match strings between outermost left and right delimiters, or an array of
757
- objects with detailed match parts and position data. An error is thrown if delimiters are
758
- unbalanced within the data .</ p >
757
+ objects with detailed match parts and position data. By default, an error is thrown if
758
+ delimiters are unbalanced within the subject string .</ p >
759
759
760
760
< table cellspacing ="0 " class ="api ">
761
761
< tbody >
@@ -775,7 +775,33 @@ <h2 id="matchRecursive"><code>XRegExp.matchRecursive(<span class="plain">str, le
775
775
Any combination of XRegExp flags, used for the left and right delimiters.
776
776
</ li >
777
777
< li > [< code > options</ code > ] {< code > Object</ code > }< br />
778
- Lets you specify < code > valueNames</ code > and < code > escapeChar</ code > options.
778
+ Options object with optional properties:
779
+ < ul >
780
+ < li > < code > valueNames</ code > {< code > Array</ code > } Providing < code > valueNames</ code > changes the return value from an array of
781
+ matched strings to an array of objects that provide the value and start/end positions
782
+ for the matched strings as well as the matched delimiters and unmatched string segments.
783
+ To use this extended information mode, provide an array of 4 strings that name the parts
784
+ to be returned:
785
+ < ol >
786
+ < li > String segments outside of (before, between, and after) matches.</ li >
787
+ < li > Matched outermost left delimiters.</ li >
788
+ < li > Matched text between the outermost left and right delimiters.</ li >
789
+ < li > Matched outermost right delimiters.</ li >
790
+ </ ol >
791
+ Taken together, these parts include the entire subject string if used with flag < code > g</ code > .< br />
792
+ Use < code > null</ code > for any of these values to omit unneeded parts from the returned results.
793
+ </ li >
794
+ < li > < code > escapeChar</ code > {< code > String</ code > } Single char used to escape delimiters within the subject string.</ li >
795
+ < li > < code > unbalanced</ code > {< code > String</ code > } Handling mode for unbalanced delimiters. Options are:
796
+ < ul >
797
+ < li > < code > 'error'</ code > - throw (default)</ li >
798
+ < li > < code > 'skip'</ code > - unbalanced delimiters are treated as part of the text between delimiters, and
799
+ searches continue at the end of the unbalanced delimiter.</ li >
800
+ < li > < code > 'skip-lazy'</ code > - unbalanced delimiters are treated as part of the text between delimiters,
801
+ and searches continue one character after the start of the unbalanced delimiter.</ li >
802
+ </ ul >
803
+ </ li >
804
+ </ ul >
779
805
</ li >
780
806
</ ul >
781
807
</ td >
@@ -794,13 +820,13 @@ <h2 id="matchRecursive"><code>XRegExp.matchRecursive(<span class="plain">str, le
794
820
795
821
< h3 > Example</ h3 >
796
822
< pre class ="sh_javascript "> // Basic usage
797
- let str = '(t((e))s)t()(ing)';
798
- XRegExp.matchRecursive(str , '\\(', '\\)', 'g');
823
+ const str1 = '(t((e))s)t()(ing)';
824
+ XRegExp.matchRecursive(str1 , '\\(', '\\)', 'g');
799
825
// -> ['t((e))s', '', 'ing']
800
826
801
827
// Extended information mode with valueNames
802
- str = 'Here is <div> <div> an</div> </div> example';
803
- XRegExp.matchRecursive(str , '<div\\s*> ', '</div> ', 'gi', {
828
+ const str2 = 'Here is <div> <div> an</div> </div> example';
829
+ XRegExp.matchRecursive(str2 , '<div\\s*> ', '</div> ', 'gi', {
804
830
valueNames: ['between', 'left', 'match', 'right']
805
831
});
806
832
/* -> [
@@ -812,8 +838,8 @@ <h3>Example</h3>
812
838
] */
813
839
814
840
// Omitting unneeded parts with null valueNames, and using escapeChar
815
- str = '...{1}.\\{{function(x,y){return {y:x}}}';
816
- XRegExp.matchRecursive(str , '{', '}', 'g', {
841
+ const str3 = '...{1}.\\{{function(x,y){return {y:x}}}';
842
+ XRegExp.matchRecursive(str3 , '{', '}', 'g', {
817
843
valueNames: ['literal', null, 'value', null],
818
844
escapeChar: '\\'
819
845
});
@@ -825,9 +851,16 @@ <h3>Example</h3>
825
851
] */
826
852
827
853
// Sticky mode via flag y
828
- str = '<1> <<<2> > > <3> 4<5> ';
829
- XRegExp.matchRecursive(str , '<', '> ', 'gy');
854
+ const str4 = '<1> <<<2> > > <3> 4<5> ';
855
+ XRegExp.matchRecursive(str4 , '<', '> ', 'gy');
830
856
// -> ['1', '<<2> > ', '3']
857
+
858
+ // Skipping unbalanced delimiters instead of erroring
859
+ const str5 = 'Here is <div> <div> an</div> unbalanced example';
860
+ XRegExp.matchRecursive(str5, '<div\\s*> ', '</div> ', 'gi', {
861
+ unbalanced: 'skip'
862
+ });
863
+ // -> ['an']
831
864
</ pre >
832
865
833
866
0 commit comments