RegEE
Description
Regular Expression with Extended functionality. RegEE JavaScript module. This module allows the use of named groups in a regular expression.
Changes
- 0.4.0
- changed importing syntax
- added methods: match and replace
Installing module
C:\npm install regee
Usage in code
// for simple import;//or; // for using as classconst RegEE = // or
Syntax
# | method | syntax | meaning |
---|---|---|---|
1 | ematch | string.ematch( pattern[, flags] ) | matching string and returns an array |
2 | match | string.match( new RegEE( pattern[, flags] ) ) | matching string and returns an array |
3 | ereplace | string.ereplace( pattern, replacer[, flags] ) | replace string and return it |
4 | replace | string.replace( new RegEE( pattern[, flags] ), replacer ) | replace string and return it |
Parameters
# | parameter | description |
---|---|---|
1 | pattern | Required. The string or regular expression as a string that will be replaced by the replacer. |
2 | replacer | Required. New substring or function. |
3 | flags | Optional. One of the javascript regexp flags and the added x flag. |
syntax sample:
// returns an array of matches foundstr// orstr// returns the result of the replacement as a stringstr// orstr
Flag x
# | character | meaning |
---|---|---|
1 | x | ignore whitespace |
example:
var str = 'My name is John Smith. I am 25 year old'; // or ------------------------------------------------------var result = str; // or ------------------------------------------------------var result = str;
Pattern
Named group
# | regexp | meaning |
---|---|---|
1 | (?<somename>\w+) | Named group |
example:
var str = 'My name is John Smith. I am 25 year old'; // or --------------------------------------------------------------------------------// name of group// |var result = str; // or --------------------------------------------------------------------------------var result = str;
Named back reference
# | regexp | meaning |
---|---|---|
1 | \k<somename> | Back reference for named group |
2 | \g<somename> | Back reference for named group |
example:
var str = 'to be or not to be'; // or --------------------------------------------------------------------------------// name of group <---------------- backreference// | |var isHamlet = str; // or --------------------------------------------------------------------------------var isHamlet = str
Replacer
...as string
String to replace. The string can contain the result value taken from the capture group.
# | syntax | meaning |
---|---|---|
1 | $+{ groupName } | captured value from the named group |
example:
var oldString = 'My name is John Smith. I am 25 year old'; // or --------------------------------------------------------------------------------var newString = str; // or --------------------------------------------------------------------------------var newString = str; // result ----------------------------------------------------------------------------console; // --> John: 25
...as function
The function takes two parameters. The first parameter is a matching string, and the second is an array of captured values. The function returns a string to replace the match string.
# | syntax | meaning |
---|---|---|
1 | function( string, groups ) { ...some code; return '...some string'; } | return a string for replacement |
2 | ( string, groups ) => { ...some code; return '...some string'; } | return a string for replacement |
example:
var oldString = 'My name is John Smith. I am 25 year old.'; // or --------------------------------------------------------------------------------var newString = str; // or --------------------------------------------------------------------------------var newString = str; // or --------------------------------------------------------------------------------var newString = str; // result ----------------------------------------------------------------------------console; // --> John: 25
Examples
example matching:
var str = 'My name is John Smith. I am 25 year old. My name is Jasmine. I am 32 year old.';//You can using method 'ematch' for String objectsvar result = str;console; // --> Johnconsole; // --> Johnconsole; // --> Jasmineconsole; // see down.../*This action returns arrays, the number of arrays is equal to the number of matches.The first element of each array contains a string that matches the pattern.result is...[['My name is John Smith. I am 25 year old','John','Smith','25',FirstName: 'John',LastName: 'Smith',Age: '25'],['My name is Jasmine. I am 32 year old','Jasmine',undefined,'32',FirstName: 'Jasmine',LastName: undefined,Age: '32']]*/
example replacing:
var str = 'My name is John Smith. I am 25 year old. My name is Jasmine. I am 32 year old.';var newString = str;console; // see down.../*John: 25Jasmine: 32*/
Author
Khalid Dudaev