Class: RegexHighlighter

RegexHighlighter

new RegexHighlighter(returnClassNameopt)

A class for the Regex Highlighter
Parameters:
Name Type Attributes Description
returnClassName string <optional>
The class name to add to the span tags once a pattern has been matched
Author:
Source:

Methods

assembleNewStringFromMatchArray(string, array)

Produces a string which contains the new highlighting, by wrapping matches in the passed array in a span tag with the corresponding class. In order to make sure this functions correctly, it uses an offset and a pre-built match array. When actually wrapping a match with a span, the method wrapTextWithSpan is used.
Parameters:
Name Type Description
string string The string which will be highlighted via the match array
array Array.<Match> The array which contains all the match information
Author:
Source:

defaultDuplicateFunction(a, b)

Default duplicate function for the insertSyntaxHighlighting function
Parameters:
Name Type Description
a Match This is the left regex match
b Match This is the right regex match
Author:
Source:

findRegexMatches(string, regexString, captureGroup, type, precedence)

This function finds all of the regex matches in a string from a regex string, it will collect only the elements in the specified capture group. An array containing Match Objects will be returned.
Parameters:
Name Type Description
string string The string to search
regexString string The regex string to use
captureGroup number The capture group to collect
type string The type class that will given to the Match object so that it can be individually styled
precedence number The precedence to be given to the Match Object
Author:
Source:

getMatchesArrayFromRegex(regexObject, string)

This function will return all of the matches that an object which contains different regex patterns will match with a string
Parameters:
Name Type Description
regexObject Object This is the object which contains all of the regex information. It can be loaded from a file, see JSON examples for how to create these.
string string This is the text that needs to be converted to its highlighted form
Author:
Source:

insertSyntaxHighlighting(regexObject, string, duplicateFunctionopt)

This function inserts the span tags into the string and returns a new string which can be added to the page or printed to console. This is the main function for where all of the sub functions are called. As well as where the main duplicateFunction is defined if one is not passed in.
Parameters:
Name Type Attributes Description
regexObject Object This is the object which contains all of the regex information. It can be loaded from a file, see JSON examples for how to create these.
string string This is the text that needs to be converted to its highlighted form.
duplicateFunction function <optional>
This is the function which will be used to remove any duplicate matches from the highlighting.
Author:
Source:

insertSyntaxHighlightingByClass(regexObject, classNameopt)

Loads elements from the document via a className like the loadSyntaxHighlightingByClass but this function will use a regexObject directly from code, instead of loading one from a directory. If no className is given to load the elements, then it will use the default of 'regex-color'.
Parameters:
Name Type Attributes Description
regexObject string This is the object that will be used to highlight the text page elements need to be highlighted.
className string <optional>
This is the className that will be used to identify what elements on the page need to be highlighted
Author:
Source:

loadSyntaxHighlightingByClass(classNameopt, languagesFolderPathopt)

The main function to add regex-highlighting to any element on the page. The elements will be loaded by a class supplied to the function, then they will have their innerHTML highlighted by inserting span tags with the the default class of 'regex-color'. The regex languages will be searched for in the specified languagesFolderPath variable, if no path is given, then the default path will be './languages/'.
Parameters:
Name Type Attributes Description
className string <optional>
The className that will be used to identify which page elements need to be highlighted.
languagesFolderPath string <optional>
This is the path to the languages folder, so that any languages that can be used, will be found
Author:
Source:

removeDuplicateObjectsFromArray(array, shouldRemove)

Remove duplicate objects from the array using a function passed into this function. The array should contain Regex match objects and the shouldRemove function should work in a similar way to a compare function in most imperative languages. This means that <0 will remove the item on the left, >0 will remove the item on the right. 0 will not remove anything
Parameters:
Name Type Description
array Array.<Match> The array which will have its duplicate items removed from
shouldRemove function A callback function to decide what will be removed
Author:
Source:

sortArrayByObjectsIndex(array)

A function to sort the values of the passed in array, first by their indicies then by their precedence.
Parameters:
Name Type Description
array Array.<Match> Array of regex match objects
Author:
Source:

wrapTextWithSpan(text, classesopt, startopt, endopt)

Wraps a given piece of text with a span tag that has a class.
Parameters:
Name Type Attributes Description
text string The text as a whole
classes string <optional>
The classes to give the wrapping
start number <optional>
The start point of the wrapping
end number <optional>
The end point of the wrapping
Author:
Source: