RegExp pattern / replace function

Joined
Sep 4, 2022
Messages
155
Reaction score
16
Hello Helpers ,

I have a regExp pattern to define, and one array to crawl with its content as substitute for all slot found.
I am approaching a good solution , but am not enough expert at it.



JavaScript:
<script>

var list = [["a","b"],["c","d"],["e","f"]] ; // content Array, at goal it could have 10 vars in each sub_arrays

var pattern = "%1 %1 %2 %1" ; // string target, requiring all replaces for each %var

function build(feed,pattern){

    outraw = "" ;

    for(let i=0;i < feed.length;i++){

        // below : my try about setting the RegExp pattern
        outraw += pattern.replace( new RegExp(["%1","%2"],"i") , feed[i][0] , feed[i][1] ) ;
        outraw += "<br>" ;

    }

    alert(outraw);

    document.body.innerHTML += outraw ;
    outraw = null;

}

</script>
</head>
<body onload="build(list,pattern);">

My goal is to use 2 dimensional Array, and wrap it by the pattern/scheme from 'pattern var'.

I am stuck at 'having the good' regexp , and the relevant use of the 'replace' function.

the regexp pattern require to pick all '%integers' , and replace them by the good feed[index]

thanks in advance :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,283
Messages
2,571,405
Members
48,098
Latest member
inno vation

Latest Threads

Top