Attempt to reverse a regular expression


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}"
Fab
Latest posts by Fab (see all)

About Fab

Solutions Architect, I build great workflows for the news, media and broadcast industries. I play with data too.

Leave a comment

Your email address will not be published. Required fields are marked *