/*global Autolinker */ /** * @class Autolinker.matcher.Mention * @extends Autolinker.matcher.Matcher * * Matcher to find/replace username matches in an input string. */ Autolinker.matcher.Mention = Autolinker.Util.extend( Autolinker.matcher.Matcher, { /** * Hash of regular expression to match username handles. Example match: * * @asdf * * @private * @property {Object} matcherRegexes */ matcherRegexes : { "twitter": new RegExp( '@[_' + Autolinker.RegexLib.alphaNumericCharsStr + ']{1,20}', 'g' ), "instagram": new RegExp( '@[_.' + Autolinker.RegexLib.alphaNumericCharsStr + ']{1,50}', 'g' ) }, /** * The regular expression to use to check the character before a username match to * make sure we didn't accidentally match an email address. * * For example, the string "asdf@asdf.com" should not match "@asdf" as a username. * * @private * @property {RegExp} nonWordCharRegex */ nonWordCharRegex : new RegExp( '[^' + Autolinker.RegexLib.alphaNumericCharsStr + ']' ), /** * @constructor * @param {Object} cfg The configuration properties for the Match instance, * specified in an Object (map). */ constructor : function( cfg ) { Autolinker.matcher.Matcher.prototype.constructor.call( this, cfg ); this.serviceName = cfg.serviceName; }, /** * @inheritdoc */ parseMatches : function( text ) { var matcherRegex = this.matcherRegexes[this.serviceName], nonWordCharRegex = this.nonWordCharRegex, serviceName = this.serviceName, tagBuilder = this.tagBuilder, matches = [], match; if (!matcherRegex) { return matches; } while( ( match = matcherRegex.exec( text ) ) !== null ) { var offset = match.index, prevChar = text.charAt( offset - 1 ); // If we found the match at the beginning of the string, or we found the match // and there is a whitespace char in front of it (meaning it is not an email // address), then it is a username match. if( offset === 0 || nonWordCharRegex.test( prevChar ) ) { var matchedText = match[ 0 ].replace(/\.+$/g, ''), // strip off trailing . mention = matchedText.slice( 1 ); // strip off the '@' character at the beginning matches.push( new Autolinker.match.Mention( { tagBuilder : tagBuilder, matchedText : matchedText, offset : offset, serviceName : serviceName, mention : mention } ) ); } } return matches; } } );