Reverse a regex in javascript ?
function reverse(s)
{
return s.split('').reverse().join('');
}
function reverseSequence (s) {
// chopper les expression conditionnelles
// et construire un tableau pour les stocker
var conditionals1 = s.match(/\[(.*?)\]/g);
var conditionals2 = s.match(/(\[(.+)\])+\{(.*?)\}/g);
var conditionals3 = s.match(/[A-Z\.]{1}\{(.*?)\}/g);
var conditionals4 = s.match(/\^(.)/g);
// on reverse la sequence brutalement
s = reverse(s);
// on replace les expressions conditionnelles
if(conditionals3)
for(let i=0; i<conditionals3.length; i++)
{
s = s.replace(reverse(conditionals3[i]), conditionals3[i]);
}
if(conditionals2)
for(let i=0; i<conditionals2.length; i++)
{
s = s.replace(reverse(conditionals2[i]), conditionals2[i]);
}
if(conditionals1)
for(let i=0; i<conditionals1.length; i++)
{
s = s.replace(reverse(conditionals1[i]), conditionals1[i]);
}
if(conditionals4)
for(let i=0; i<conditionals4.length; i++)
{
s = s.replace(reverse(conditionals4[i]), conditionals4[i]);
}
return s;
}
proteine = "[AVGIL]{0,5}K^U.[DE]";
reverseSequence(proteine);
>> "[DE].^UK[AVGIL]{0,5}"
Latest posts by Fab (see all)
- Frankenstein: review (and the truth) - 15 November 2025
- Voyage au bout de la nuit: review - 15 October 2025
- 2025 New Years Eve Party - 29 September 2025
