skip to Main Content

I am having beginners trouble with comma separation from numbers extracted using regular expressions. The exercise is as follows:

Find and print all the digits from the sentence as a single array. If the sentence has no digits, print "No digits!". Try to do the check using the test()-method.

This is my result: Screenshot of the code and result

var sentence = "5om3 wr173 w0rd5 u51n9 numb3r5"

function printDigits() {
  var res = sentence.replace(/D/g, '');
  console.log(res)
}

printDigits();

4

Answers


  1. You can get comma separation by converting the string to an array and then joining seperated by commas with the join method

    var sentence = "5om3 wr173 w0rd5 u51n9 numb3r5"
    
    function printDigits() {
      var res = sentence.replace(/D/g, '');
      console.log(Array.from(res).join(','))
    }
    
    printDigits();
    Login or Signup to reply.
  2. You could spread the final number to get an array, then use join() to add those comma’s. (join uses , as the default separator

    As a fallback, you can use OR || to set the value of res to a string you like

    var sentence = "5om3 wr173 w0rd5 u51n9 numb3r5"
    
    function printDigits() {
        var res = [ ...sentence.replace(/D/g, '') ].join() || "No digits!";
        console.log(res)
    }
    
    printDigits();
    Login or Signup to reply.
  3. If you follow the question’s prompt to use test() this becomes a matter of testing each character, rather than manipulating the string.

    In long hand you can iterate over the string itself, here using a for…of loop

    function printDigits(str) {
      const res = [];
    
      for (const char of str) {
        if (/d/.test(char)) {
          res.push(char);
        }
      }
    
      return res.length ? res.join(', ') : 'No digits!';
    }
    
    const sentence = '5om3 wr173 w0rd5 u51n9 numb3r5';
    
    console.log(printDigits(sentence));
    console.log(printDigits('This has no digits.'));

    This can be shortened somewhat using spread syntax to convert the string to an array and then the array filter() method to only keep those characters that pass the test().

    function printDigits(str) {
      const res = [...str].filter((char) => /d/.test(char));
      return res.length ? res.join(', ') : 'No digits!';
    }
    
    const sentence = '5om3 wr173 w0rd5 u51n9 numb3r5';
    
    console.log(printDigits(sentence));
    console.log(printDigits('This has no digits.'));
    Login or Signup to reply.
  4. Instead of replacing characters use match which returns an array based on a regex pattern. You can even pass in an argument specifying if you want to return an array of strings or numbers.

    const sentence1 = '5om3 wr173 w0rd5 u51n9 numb3r5';
    const sentence2 = 'This sentence has no digits';
    
    function printDigits(sentence, type = 'str') {
      const match = sentence.match(/d/g);
      if (!match) return 'No digits';
      return type === 'num' ? match.map(Number) : match;
    }
    
    console.log(printDigits(sentence1, 'num'));
    console.log(printDigits(sentence1));
    console.log(printDigits(sentence2));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search