| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- "use strict";
- var XRegExp = require('xregexp').XRegExp,
- LeafMatchExpression = require('./LeafMatchExpression'),
- ErrorCodes = require('../../Errors').ErrorCodes;
- var RegexMatchExpression = module.exports = function RegexMatchExpression(){
- base.call(this, 'REGEX');
- }, klass = RegexMatchExpression, base = LeafMatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
- klass.MaxPatternSize = 32764;
- proto._flags = undefined;
- proto._re = undefined;
- proto._regex = undefined;
- /**
- *
- * Writes a debug string for this object
- * @method debugString
- * @param level
- *
- */
- proto.debugString = function debugString(level) {
- var debug = this._debugAddSpace( level );
- debug += this.path() + " regex /" + this._regex + "/" + this._flags;
- var td = this.getTag();
- if (td === null) {
- debug += " " + td.debugString();
- }
- debug += "\n";
- return debug;
- };
- proto.shortDebugString = function shortDebugString() {
- return "/" + this._regex + "/" + this._flags;
- };
- /**
- *
- * checks if this expression is == to the other
- * @method equivalent
- * @param other
- *
- */
- proto.equivalent = function equivalent(other) {
- if (this.matchType() !== other.matchType()) return false;
-
- return this.path() === other.path() && this._regex === other._regex && this._flags === other._flags;
- };
- /**
- *
- * Return the _flags property
- * @method getFlags
- *
- */
- proto.getFlags = function getFlags(){
- return this._flags;
- };
- /**
- *
- * Return the _regex property
- * @method getString
- * @param
- *
- */
- proto.getString = function getString(){
- return this._regex;
- };
- /**
- *
- * Initialize the necessary items
- * @method init
- * @param path
- * @param type
- *
- */
- proto.init = function init(path,regex,flags) {
- if(regex.toString().length > klass.MaxPatternSize){
- return {'code':ErrorCodes.BAD_VALUE, 'desc':'Regular Expression too long.'};
- }
- if (regex instanceof RegExp){
- this._regex = regex.source;
- this._re = regex;
- this._flags = (this._re.ignoreCase ? 'i' : '') + (this._re.multiline ? 'm' : '');
- } else if (typeof regex === 'string' && (!flags || typeof flags === 'string' )) {
- this._regex = regex;
- //remove invalid flags, sort and uniquify them
- this._flags = (flags || '').replace( /[^imxs]/g, '').split('').sort().filter(function(el,i,a){return i===a.indexOf(el);}).join('');
- this._re = new XRegExp(regex,this._flags);
- } else {
- return {'code':ErrorCodes.BAD_VALUE, 'desc':'regex not a regex'};
- }
- return this.initPath( path );
- };
- /**
- *
- * Check if the input element matches
- * @method matchesSingleElement
- * @param e
- *
- */
- proto.matchesSingleElement = function matchesSingleElement(e) {
- if(e instanceof RegExp){
- return e.toString() === this._re.toString();
- }
- if(typeof e === 'string'){
- return this._re.test(e);
- }
- // No support for SYMBOLS currently
- };
- /**
- *
- * clone this instance to a new one
- * @method shallowClone
- *
- */
- proto.shallowClone = function shallowClone(){
- var e = new RegexMatchExpression();
- e.init( this.path(), this._regex, this._flags );
- if ( this.getTag() ) {
- e.setTag(this.getTag().clone());
- }
- return e;
- };
|