Quantcast

[Moo] preg_replace in javascript?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Moo] preg_replace in javascript?

Eneko Alonso
This is not Mootools related, but Mootols uses a lot of regex, so I wonder if you guys know how to do preg_replace in javascript, this is, using matched results in the replacement string.
From what I know, string.replace can do regex for matching, but not for replacing, thus, the replace string has to be static.

Any ideas?

Thanks a lot.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Moo] preg_replace in javascript?

Fabio M. Costa

you mean like:

'fabio'.replace(/(fa)/g, '$1a'); // return "faabio"

--
Fábio Miranda Costa
Solucione Sistemas
Engenheiro de interfaces


On Thu, Mar 11, 2010 at 4:36 PM, Eneko Alonso <[hidden email]> wrote:
This is not Mootools related, but Mootols uses a lot of regex, so I wonder if you guys know how to do preg_replace in javascript, this is, using matched results in the replacement string.
From what I know, string.replace can do regex for matching, but not for replacing, thus, the replace string has to be static.

Any ideas?

Thanks a lot.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Moo] preg_replace in javascript?

Fabio M. Costa
so explaining the example:

'fabio'.replace(/(fa)/g, '$1a'); // returns "faabio"

Ive created one group (fa) and then on the replace string i reference it by using a dollar sign followed by its index, which is 1 (0 is for the complete match, which is 'fa' too hehe).

You can reference a group into the current regex too, like this:

'fafafabio'.replace(/(fa)\1\1/g, '$1a'); // returns "faabio" too

Just replace the dollar sign by a backslash, the index works the same way.

--
Fábio Miranda Costa
Solucione Sistemas
Engenheiro de interfaces


2010/3/11 Fábio M. Costa <[hidden email]>

you mean like:

'fabio'.replace(/(fa)/g, '$1a'); // return "faabio"

--
Fábio Miranda Costa
Solucione Sistemas
Engenheiro de interfaces



On Thu, Mar 11, 2010 at 4:36 PM, Eneko Alonso <[hidden email]> wrote:
This is not Mootools related, but Mootols uses a lot of regex, so I wonder if you guys know how to do preg_replace in javascript, this is, using matched results in the replacement string.
From what I know, string.replace can do regex for matching, but not for replacing, thus, the replace string has to be static.

Any ideas?

Thanks a lot.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Moo] preg_replace in javascript?

Eneko Alonso
That is very cool, Fabio, thanks a lot.


2010/3/11 Fábio M. Costa <[hidden email]>
so explaining the example:

'fabio'.replace(/(fa)/g, '$1a'); // returns "faabio"

Ive created one group (fa) and then on the replace string i reference it by using a dollar sign followed by its index, which is 1 (0 is for the complete match, which is 'fa' too hehe).

You can reference a group into the current regex too, like this:

'fafafabio'.replace(/(fa)\1\1/g, '$1a'); // returns "faabio" too

Just replace the dollar sign by a backslash, the index works the same way.


--
Fábio Miranda Costa
Solucione Sistemas
Engenheiro de interfaces


2010/3/11 Fábio M. Costa <[hidden email]>


you mean like:

'fabio'.replace(/(fa)/g, '$1a'); // return "faabio"

--
Fábio Miranda Costa
Solucione Sistemas
Engenheiro de interfaces



On Thu, Mar 11, 2010 at 4:36 PM, Eneko Alonso <[hidden email]> wrote:
This is not Mootools related, but Mootols uses a lot of regex, so I wonder if you guys know how to do preg_replace in javascript, this is, using matched results in the replacement string.
From what I know, string.replace can do regex for matching, but not for replacing, thus, the replace string has to be static.

Any ideas?

Thanks a lot.



Loading...