1

I am not able to fix the following:

<html>
<head>
    <title>Test</title>

<script>
   function myFunction(arr) {
     var N = [0,1,2,3,4,5,6,7,8,9];
     for (var s in N) {

     var pattern = new RegExp('/^[A-z]et_[' + s + ']_\\d/');
     // NOTE I get the following, is that how it should be?
     // window.alert(pattern);  =>  /\/^[A-z]et_[0]_\d\//
     // NOTE: I checked that it works fine for a single digit case, say:
     // var pattern = new RegExp(/^[A-z]et_[3]_\d/);  
    var newarr = arr.filter(elt => pattern.test(elt));
    document.getElementById("demo").innerHTML = newarr;
    }}
</script>

</head>
<body>
<div id="demo" onclick="myFunction(['aaaLet_0_0', 'Let_1_99', 'Let_2_',   'Let_3_99', 'Pet_2_', 'Pet_3_99', '_9_33']);">click here</div>
<hr>
<p>expected output: Let_1_99, Let_3_99, Pet_3_99</p>
</body>
</html>

I see many similar questions, but I have not been able to find out how to fix my code. It should be possible, shouldn't it?

EDIT the following is what I need:

 <html>
 <head>
 <title>Test</title>
 <script>

 var newarr = [];
function myFunction(arr) {
    var N = [0,1,2,3,4,5,6,7,8,9];
    for (var s in N) {

    var pattern = new RegExp('^[A-z]et_' + s + '_\\d'); // NOTE: not /
    var lnewarr = arr.filter(elt => pattern.test(elt));
    if(typeof lnewarr !== 'undefined' && lnewarr.length > 0){newarr.push(lnewarr)};
    }
    document.getElementById("demo").innerHTML = newarr;
    }
   </script>

  </head>
  <body>
  <div id="demo" onclick="myFunction(['aaaLet_0_0', 'Let_1_99', 'Let_2_', 'Let_3_99', 'Pet_2_', 'Pet_3_99', '_9_33']);">click here</div>
  <hr>
  <p>expected output: Let_1_99, Let_3_99, Pet_3_99</p>
  </body>
  </html>

(sorry if my post is mostly code. Hope it may helps somebody out there.)

1
  • I guess I don't understand why the regex is not simply /^[A-z]et_\d_\d/ ... this ... var pattern = /^[A-z]et_\d_\d/; / var arr = ['aaaLet_0_0', 'Let_1_99', 'Let_2_', 'Let_3_99', 'Pet_2_', 'Pet_3_99', '_9_33']; / var lnewarr = arr.filter(elt => pattern.test(elt)); / produces: ["Let_1_99", "Let_3_99", "Pet_3_99"] Commented Jan 28, 2017 at 0:20

1 Answer 1

1

Instead of for loop you can use a .join like this:

var pattern = new RegExp('^[A-Za-z]et_[' + N.join('') + ']_\\d');
//=> /^[A-Za-z]et_[0123456789]_\d/

This works for the case when you have singe character values in your array N.

For generic use you can use this expression:

var pattern = new RegExp('^[A-Za-z]et_(?:' + N.join('|') + ')_\\d');
//=> /^[A-Za-z]et_(?:0|1|2|3|4|5|6|7|8|9)_\d/

Using a for loop you can do this:

var N = [0,1,2,3,4,5,6,7,8,9];
var str='(?:';
for (var s in N)
   str += s + '|';    
str = str.replace(/\|$/, ')');

var pattern = new RegExp('^[A-Za-z]et_' + str + '_\\d');
//=> /^[A-Za-z]et_(?:0|1|2|3|4|5|6|7|8|9)_\d/
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it works, thanks. Let me ask, what is wrong with the for loop? Is there a way to keep it? (Example in my question is from a larger piece of code, and therein I need to have a for loop).
@anubhave, the point I was confused is how we write a regexp within RegExp(). It seems that I should avoid '/'. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.