#252 ✓resolved
Marton Kiss-Albert

String#gsub not supported fully string pattern

Reported by Marton Kiss-Albert | July 29th, 2008 @ 05:59 PM | in 1.7

The gsub function not supported fully the string patterns

eg.:

var str='sky :-)';
var pat=':-)';
var rep='Smyle';
var out=str.gsub(pat,rep);

ERROR 'unmatched ) in regular expression'

The gsub function using regExp.match() function, also match() function using regular expression.

Patch:

Object.extend(String.prototype, {
  gsub: function(pattern, replacement) {
    var result = '', source = this, match;
    replacement = arguments.callee.prepareReplacement(replacement);
	
	// INSERTED LINE: --------------------
	if(Object.isString(pattern)) pattern=pattern.replace(/([.\\+*?[\]^$(){}=!<>|:])/g,'\\$1');
	// END OF INSERTION ------------------

    while (source.length > 0) {
      if (match = source.match(pattern)) {
        result += source.slice(0, match.index);
        result += String.interpret(replacement(match));
        source  = source.slice(match.index + match[0].length);
      } else {
        result += source, source = '';
      }
    }
    return result;
  },
...}

(Sorry my English)

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

Attachments

Pages