Regular match parentheses (the number is uncertain), and then add an index to each parenthesis to pass to the background

fill in the blanks, the question cadres are divided into uncertain blanks, which are determined by parentheses, and the left and right parentheses are translated as! =!. Then this blank needs to pass a key value to the background to facilitate the storage of the answer under the parentheses.
for example: how abcd () efgh () ijkl () mn () becomes abcd (- sharp1) efgh (- sharp2) ijkl (- sharp3) mn (- sharp4)
an index needs to be added to the parentheses

Mar.11,2021

Let me give you a general idea:

  1. cut abcd () efgh () ijkl () mn () with () and cut into character arrays
  2. then the character array is looped to generate (- sharp serial number) and spliced into abcd (- sharp1) efgh (- sharp2) ijkl (- sharp3) mn (- sharp4)

Link description

    var str = 'abcd()efgh()ijkl()mn()';
    var n_str = str.replace(/(\w+)\(\)/g, '$1,');
    var ary = n_str.split(',');
    var result = '';
    ary.splice(ary.length-1, 1);
    ary.forEach(function(ele, index) {
        result += ele + '(-sharp' + (index + 1) + ')';
    });
    console.log(result);

the above ideas are all right, and I have adopted a method similar to the second method in my project:

     const str = 'abcd()efgh()ijkl()mn()';
     let index = 0;
     function replacer() {
       indexPP;
       return ['!=', index, '=!'].join(' ');
     }
     let strTrans = str.replace(/([(])(\s*)([)])/g, replacer);
     console.log(strTrans);
Menu