Class: Regex

Regex

new Regex(patternopt, optionsopt)

This class is an extension of the standard RegExp class adding missing functionality. For further descriptions see the corresponding overridden methods.
Parameters:
Name Type Attributes Description
pattern string | RegExp <optional>
options string <optional>
Source:
See:

Methods

(static) match(stropt) → {Object}

Full detailed match. Based on RegExp[Symbol.match] with some improvements regarding the return value. Analogue to the base method RegExp[Symbol.match] internally the method Regex#exec is called. And there are 2 improvements related to this: First improvement is that Regex#exec returns the index for all matching groups and Second improvement is that we always return a 2-dimensional array independent of whether the global 'g' flag is set or not. If the 'g' flag is not set then first dimension only contains one element which is the result of Regex#exec and if the flag is set the size of the first dimension equals the number of matches we had and each match contains the Regex#exec result for the corresponding execution.
Parameters:
Name Type Attributes Description
str String <optional>
Source:
See:
Returns:
Array[{string[0..n], index:number[0..n], input:string}]
Type
Object

(static) replace(stropt, newSubstringFunctionArrayopt) → {string}

Group based search & replace. Based on Regex.prototype[Symbol.replace] but instead of only being able to provide a single "replacement substring" or "replacement function" for replacement of the entire match (aka matching-group[0]) we can provide an array [0..n] of "replacement substring" or "replacement function" elements for replacement of each matching group [0..n].
Important: If we don't want a group to be replaced we provide the corresponding "replacement substring/function" array element as undefined or null!
Important: If we provide a "replacement substring/function" for a parent group-element then (obviously) no replacement is performed on its child-group-elements. E.g. if we provide a "replacement substring/function" for group 0 then the entire match is replaced and (obviously) no replacement of the further child-groups takes place!

Syntax: (new Regex(pattern))[Symbol.replace](string, [array of replacement strings])
Alternative Syntax: For browsers supporting "Symbol": Chrome & Firefox: string.replace(new Regex(pattern), [array of replacement strings])
Example:
//Convert plain text to html: Replace special characters (multiple spaces, tabs, ...) in plain text with their html "equivalent":
var CONVERT_TEXT_SPECIAL_CHARACTER_TO_HTML_ESCAPE_CHARACTER_PATTERN = "( {2})|(\t)|(&)|(<)|(>)|(\n)";
var CONVERT_TEXT_SPECIAL_CHARACTER_TO_HTML_ESCAPE_CHARACTER_PATTERN_REPLACE_STRING = [undefined, "&nbsp;&nbsp;", "&emsp;", "&amp;", "&lt;", "&gt;", "<br>"];
var regex = new Regex(CONVERT_TEXT_SPECIAL_CHARACTER_TO_HTML_ESCAPE_CHARACTER_PATTERN, 'g');
var result = regex[Symbol.replace](myPlainText,CONVERT_TEXT_SPECIAL_CHARACTER_TO_HTML_ESCAPE_CHARACTER_PATTERN_REPLACE_STRING);
//Alternative Syntax: For browsers supporting "Symbol": Chrome & Firefox
var resultAlternative = myPlainText.replace(regex,CONVERT_TEXT_SPECIAL_CHARACTER_TO_HTML_ESCAPE_CHARACTER_PATTERN_REPLACE_STRING);


Special (in addition to the standard search and replace): We support in the "new substring" as well $0 as replacement pattern which is basically the entire match. And accordingly for the "new function" we call it as well with p0 as parameter which is the entire match.
Parameters:
Name Type Attributes Description
str String <optional>
newSubstringFunctionArray Array.<String> | Array.<function()> | String | function <optional>
Source:
See:
Returns:
Type
string

(static) search(str)

Simply invokes the inherited method RegExp[Symbol.search].
Parameters:
Name Type Description
str string
Source:
See:

(static) split(str, limitopt)

Simply invokes the inherited method RegExp[Symbol.split].
Parameters:
Name Type Attributes Description
str string
limit number <optional>
Source:
See:

exec(stropt) → {Object}

Full indexed exec method: Based on RegExp#exec but instead of simply getting "index" in the return which only tells the starting of the first group (0 group) we are getting "index[0..n]" which tells us the starting index of each matching group.
Syntax: (new Regex(pattern, flags)).exec(string) = {string[0..n], index:number[0..n], input:string}
Example:
//Retrieve content and position of: opening-, closing tags and body content for: non-nested html-tags. var pattern = '(<([^ >]+)[^>]*>)([^<]*)(<\\/\\2>)';
var str = '<html><code class="html plain">first</code><div class="content">second</div></html>';
var regex = new Regex(pattern, 'g');
var result = regex.exec(str);

console.log(5 === result.length);
console.log('<code class="html plain">first</code>'=== result[0]);
console.log('<code class="html plain">'=== result[1]);
console.log('first'=== result[3]);
console.log('</code>'=== result[4]);
console.log(5=== result.index.length);
console.log(6=== result.index[0]);
console.log(6=== result.index[1]);
console.log(31=== result.index[3]);
console.log(36=== result.index[4]);

Parameters:
Name Type Attributes Description
str string <optional>
Source:
See:
Returns:
{string[0..n], index:number[0..n], input:string}
Type
Object

test(stropt) → {boolean}

Simply invokes the inherited method RegExp#test.
Parameters:
Name Type Attributes Description
str string <optional>
Source:
See:
Returns:
Type
boolean

toString() → {string}

Returns the same string format as RegExp#toString for the initial pattern that was provided to the Regex constructor.
The string is constructed as follows: "/"+Regex#source+"/"+Regex#flags
Source:
Returns:
Type
string