How to use the xregexp.XRegExp.replace function in xregexp

To help you get started, we’ve selected a few xregexp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github apostrophecms / apostrophe / lib / modules / apostrophe-utils / lib / api.js View on Github external
self.truncatePlaintext = function(str, length, pruneStr){
    if (str == null) return '';

    str = String(str); length = ~~length;
    pruneStr = pruneStr != null ? String(pruneStr) : '...';

    if (str.length <= length) return str;

    var r = '.(?=\W*\w*$)';
    var regex = new XRegExp(r, 'g');

    var tmpl = function(c){ return c.toUpperCase() !== c.toLowerCase() ? 'A' : ' '; },
      template = str.slice(0, length+1);
      template = XRegExp.replace(template, regex, tmpl); // 'Hello, world' -> 'HellAA AAAAA'

    if (template.slice(template.length-2).match(/\w\w/))
      template = template.replace(/\s*\S+$/, '');
    else
      template = _.str.rtrim(template.slice(0, template.length-1));

    return (template+pruneStr).length > str.length ? str : str.slice(0, template.length)+pruneStr;
  };
github antitoxic / node-reversable-router / route.js View on Github external
//the last `masked` parameter is optional
  if (path.slice(-2) == '/*') {
    path = path.slice(0, -2) + "/**";
  }

  //escape full stops
  path = path.replace(/\./g, '\\.');

  // change wildcards , the `*` (calling them `masked` parameters) to masked0, masked1...
  var maskedWildcardName = '';
  var hasMaskedWildcard = false;
  var masked = [];
  var i = 0;

  path = XRegExp.replace(path, XRegExp('/\\*\\*?', 'g'), function (match) {
    var splatName = 'splat' + i;
    i++;
    masked.push(splatName);
    // last `splat` is optional and allows having `/` inside of it
    if (match == '/**') {
      hasMaskedWildcard = true;
      maskedWildcardName = splatName;
      return '(/(?<' + splatName + '>.*))?';
    }
    // any other `masked` parameters are required and don't allow `/` inside of them
    return '/(?<' + splatName + '>[^/]+)';
  });

  //the last `masked` parameter is optional
  if (path.slice(-1) == '/') {
    path = path.slice(0, -1);
github apostrophecms / apostrophe / lib / modules / apostrophe-utils / lib / api.js View on Github external
str = String(str); length = ~~length;
    pruneStr = pruneStr != null ? String(pruneStr) : '...';

    if (str.length <= length) return str;

    // XRegExp is different.
    // eslint-disable-next-line no-useless-escape
    var r = '.(?=\W*\w*$)';
    var regex = new XRegExp(r, 'g');

    function tmpl(c) {
      return c.toUpperCase() !== c.toLowerCase() ? 'A' : ' ';
    };

    var template = str.slice(0, length + 1);
    template = XRegExp.replace(template, regex, tmpl); // 'Hello, world' -> 'HellAA AAAAA'

    if (template.slice(template.length - 2).match(/\w\w/)) { template = template.replace(/\s*\S+$/, ''); } else { template = _.str.rtrim(template.slice(0, template.length - 1)); }

    return (template + pruneStr).length > str.length ? str : str.slice(0, template.length) + pruneStr;
  };
github antitoxic / node-reversable-router / route.js View on Github external
return '/(?<' + splatName + '>[^/]+)';
  });

  //the last `masked` parameter is optional
  if (path.slice(-1) == '/') {
    path = path.slice(0, -1);
  }

  this.hasMaskedWildcard = hasMaskedWildcard;
  this.masked = masked;
  this.maskedWildcardName = maskedWildcardName;


  // change & count named parameters ( `:params`)
  var params = [];
  path = XRegExp.replace(path, XRegExp(':[\\p{L}0-9_]+', 'g'), function (match) {
    match = match.replace(':', '');
    if (params.indexOf(match) > -1) return ':' + match;
    params.push(match);
    return '(?<' + match + '>[^/]+)';
  });
  this.params = params;


  // Check if there is optional parts in the path
  var optionalParams = [];
  var bracketGroups = [];
  var self = this;

  // Performance: only check if there's a bracket character in the path
  if (self.cleanedPath.indexOf('(') > -1) {
    var unclosed = [];
github markbirbeck / pos-chunker / lib / recursiveMatch.js View on Github external
function recursiveReplace(str, rregexp, newsubstr) {
  var regexp = new XRegExp(expand(rregexp), 'gm');
  return XRegExp.replace(str, regexp, function(match) {
    var subststr = newsubstr;

    /**
     * To emulate lookbehind a named group has been used. If it contains
     * a value then we need to juggle the results a little. For example,
     * if the parameters are:
     *
     *  replace('abc def', /(?<=abc )def/, '{$1}');
     *
     * then the callback will receive:
     *
     *  match = 'abc def'
     *  match.lookbehind = 'abc '
     *
     * We can then use these values to emulate lookbehind, by:
     *