Skip to content
Advertisement

RegExp logical problem with variables in javascript

we have some checkboxes and When each of the checkes comes true, a value will be added to a single textbox.

and when each of the checkes comes false, their own value will be deleted.

I hope I explained well.

any way… here is my code:

<script>
      function <?php echo $fff; ?>q(){
            document.getElementById('<?php echo $chid;  ?>').checked = false;
            var v;
            v=document.getElementById('<?php echo $lblid; ?>').innerText;
            var ab;
            ab=document.getElementById('ab').value;
            var t;
            let a = document.getElementById('<?php echo $idlevel; ?>').innerText;
            const b = document.getElementById('<?php echo $txtid; ?>').value;
            const c = '-' + b + '-' + a;
            var reg;
            reg= new RegExp(c, 'g');
            t=ab.replace(reg,'');
            document.getElementById('ab').value=t;
            document.getElementById('<?php echo $lblid; ?>').innerHTML='<?php echo $talent; ?>';
        }
</script>

I do know that it is not a clean code:)

btw ab is my textbox. and c is my value.

but problem is : reg= new RegExp(c, 'g'); part doesn’t accept c as value.

it works when I do this:reg= new RegExp('word', 'g');

but it doesn’t accept any variable.

pls help.

Advertisement

Answer

Try :

 const c = '-' + b + '-' + a;
 var newString = new String(c)
 var reg = new RegExp(newString, "g");
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement