#252 bug
Marton Kiss-Albert

String#gsub not supported fully string pattern

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

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 Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

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

People watching this ticket