Array contains number of elements. I need to print elements of array with strings and number with each element.
For example
Input
['1a', 'a', '2b', 'b']
Output should be
New array ['1a', '2b']
Array contains number of elements. I need to print elements of array with strings and number with each element.
For example
Input
['1a', 'a', '2b', 'b']
Output should be
New array ['1a', '2b']
2
Answers
The easiest answer is, I think:
There are refinements, of course, such as:
/.../
regular expression literal,^
: the sequence starts at the beginning of the string,d{1,2}
: a base-10 number character, which occurs a minimum of 1 time, and a maximum of 2 times (obviously the numbers can be amended to suit your specific needs),[a-z]+
: a character in the range of (lowercase) a-z, that occurs one or more times,$
: the end of the string,i
thei
flag specifies a case-insensitive search.References:
RegExp
.RegExp.prototype.test()
.Bibliography:
"Regular expressions." (A guide, MDN).
To solve this problem, you can use a regular expression to match elements that start with a number followed by any number of characters, and create a new array with those matched elements. Try to see if this code works for you.