Where is the error in replace js?

Asked

Viewed 304 times

2

t = "(10px,10px)"
t = t.replace(/-|\d+/g,3,5)

I wanted it to result in (3px,5px)

Where is the error?

3 answers

4

The replace only accepts 2 parameters:

replace(string_buscada, nova_string);

Soon your code will not work this way. A suggestion is to use match and make 2 replaces with the regex result indices:

t = "(10px,10px)";
//    ↑↑   ↑↑
//   m[0] m[1]

m = t.match(/[-|\d]+/g);
t = t.replace(m[0],3).replace(m[1],5);
console.log(t);

Another example with different values:

t = "(4px,2px)";
m = t.match(/[-|\d]+/g);
t = t.replace(m[0],3).replace(m[1],5);
console.log(t);

Edit

As raised by the friend wmsouza, this regex is not correct. If one of the numbers were negative, it would replace only the negative sign and not the whole number.

A correct regex would be:

/[-|\d]+/g

Example:

t = "(4px,-2px)";
m = t.match(/[-|\d]+/g);
t = t.replace(m[0],3).replace(m[1],5);
console.log(t);

  • A doubt. has need of -| ? https://regexr.com/3l34j

  • There is if the number is negative and you want to change to positive.

  • But you see, I’ve been testing jsbin, leaving negative number "(4px,-2px)" when executing the return is "(3px,52px)", what madness is this? rs

  • True. This regex tah bugado. I didn’t even get into the regex because it was the guy who put the question. But it’s a good thing you figured that out to add a suggestion to the answer.

  • I updated the answer

4


The @dvd has already explained the reason.

replace only accepts 2 parameters:

replace(string_buscada, nova_string);

My contribution is in the method of doing this, which would be using a callback in place of replace, thus :

var t = "(10px,10px)"
var change = [3,5];
var def = 10;
t = t.replace(/-|\d+/g,function(m){
  return change.length?change.shift():def;
});

console.log(t);

Explanation

The callback will change the catch found by the value passed on return, how I’m passing the shift of var change will remove the first value of the same and returns to replace and so on for each match found.

To var def is for case change be empty and still have matchs to be replaced.

Obs

regex for negative numbers is indeed wrong. I suggest using :

/[+-]?\d+/g
  • 1

    Who knows :D +1 ... callback!

  • vlw, @Guilhermenascimento :D

  • Raccoon vey, how amazing

  • I was impressed... Who knows... I really thank everyone.

1

The Replace has only 2 arguments.

The second argument from Replace it’s not like array of parameters. No Replace you must put an expression that represents the substitution of all parts of the regular expression in the second argument. You configure the groups you want to capture in your regular expression and use the expression $n where n is the number of the group captured.

Example below:

var t = "(10px,10px)";
var n = ["3", "5"];
var r = t.replace(/(\()\d+(px,)\d+(px\))/g, "$1"+n[0]+"$2"+n[1]+"$3");

console.log(t);
console.log(r);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.