Regular Expressions - Match Numbers and Letters of the Alphabet

Hi there i have a question about regex and the match and test functions

this code

let quoteSample = "Blueberry 3.141592653s are delicious.";

let myRegex = /[h-s2-6]/ig; // Change this line

let result = quoteSample.match(myRegex); // Change this line

is returning 17 matches which i assume are individual instances of any character that is between h-s or 2-6
is ths the same as /[h-s]|[2-6]/
and if iwanted to return a string of unknow characters and digits and unknown numbers as 1 string would i have to make a series of [a-z 0-9][repeat][repeat]…etc
as long as my string should be ?

I edited your post to make the code appear normally in the forum. (Your three backticks were not on a separate line above the code so that’s why the code didn’t get formatted correctly)

You should also post a link to the challenge so people can know the context of your question.

1 Like

The regular expression myRegex = /[h-s2-6]/ig will match any character that is between h and s or between 2 and 6 in the quoteSample string .
The i and g flags at the end of the regular expression are used to make the search case-insensitive and global (find all matches), respectively.

So yeah you understand it correctly.

The regular expression /[h-s]|[2-6]/ is equivalent to /[h-s2-6]/ because it matches any character that is either between h and s or between 2 and 6 .

To match a string of unknown characters and digits and unknown numbers as one string, you can use the regular expression /[\w\d]+/ . Here, \w matches any word character (any letter, digit, or underscore) and \d matches any digit. The + quantifier means to match one or more of the preceding pattern .

This regular expression will match any continuous sequence of word characters and digits in a string.

ah yes , thanks that makes sense

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.