Ybasthis
Ybasthis

Reputation: 81

regexp not matching all pattern

I have a regexp pattern for matching simple regexp, this can catch all pattern in text but not the first..

My pattern is

/\/((?:.| )+)\//g

My text is

|==== Données ====>
    byteCode =
    {
        id : [],
        tag : [],
        ast : []
    };
<==== Données ====|

|==== Filtres ====>
    *EspaceBlanc        => /\t|\n| /                ;;
<==== Filtres ====|

|==== Terminaux ====>
    $PrefixeId          => /#/                      ;;
    $$Mot               => /[0-9aA-zZ-]/            ;;
    $SufixeInstruction  => /;/                      ;;
    $Separateur         => /,/                      ;;
    $Enfant             => /:/                      ;;
    $$Portée            => /\|/ :: caractère.length ;;
<==== Terminaux ====|

|==== NonTerminaux ====>
    @Tag => $Mot ::
    {
        const nomTag = uniSem[0].toSource();
        const index = byteCode.tag.indexOf(nomTag);
        // Si indexTag non présent Alors index ajout byte code
        return (index >= 0)? index: (byteCode.tag.push(nomTag) - 1);
    };;

    @Id => ~$PrefixeId, $Mot :: byteCode.id.push(uniSem[1].toSource() ) - 1;

    @Elément => @Tag, [@Id] ::
    {
        const indexTag = uniSem[0].toSource();
        const id = (uniSem[1] )? uniSem[1].toSource() : -1;
        return byteCode.ast.push([indexTag, id] ) - 1;
    };;

    @InstructionSimple => @Elément, ~$SufixeInstruction :: uniSem[0].toSource();;

    @Instruction =>
    $Elément, ~$Enfant,
    [{ @Elément, ~$Separateur } ],
    @InstructionSimple ::
    {
        const idParent = uniSem[0].toSource();
        for(let i = 1, iMax = uniSem.length - 1; i < iMax; i++)
        {
            byteCode.ast[idParent].push(uniSem[i].toSource() );
        }
        // ajout de InstructionSimple
        return byteCode.ast[idParent].push(uniSem[uniSem.length - 1].toSource() );
    };
<==== NonTerminaux ====|

|==== Terminale ====>
    console.log('C\'est la fin!!');
<==== Terminale ====|

All regex are matched but this one fails:

/\t|\n| /

Source code:

const motif = /\/((?:.| )+)\//g;
//source is the text in stackoverflow description
let trouvé = null;
while(trouvé = motif.exec(source))
{
    console.log('trouvé!!', trouvé.index);
}

Result:

Expected:

https://jsfiddle.net/4bx7hboz/5/

EDIT: The pattern match on regexr.com or regex101.com. This doens't works in console because "\t" of the not find was parsed and the pattern don't match \t parsed... I reflect on a way for storing string without parsing words represent special character such as \t.

Thank you for your help and don't worry about language of my text (It isn't javascript).

Upvotes: 3

Views: 247

Answers (1)

Ybasthis
Ybasthis

Reputation: 81

After research, the strange behavior is engendred by the use of

var = '/\tstring/';

In this case the string contained by var was parsed and \t transformed in one character(code 9). For allow the regexp to make matching special word such as \t you do to don't parse string with


var = String.raw `/\tstring/\`;

In fact with this solution you can differenciate \t of special caracter code 9


If you're interested by a regex for finding regexp pattern you can use this:

/\/([^*].*)\//

Thank to @Thomas and @Bergi

https://jsfiddle.net/matthis/9mnzda3d/2/

Upvotes: 2

Related Questions