RegEx and escape sign "\" and star sign "*"

Hi, I am on Step 74 of

Learn Functional Programming by Building a Spreadsheet

JavaScript Algorithms and Data Structures (Beta)

the passed codes are following:

const highPrecedence = str => {
  const regex = /([\d.]+)([*\/])([\d.]+)/;

}

I am confused, because I thought right code should be

const highPrecedence = str => {
  const regex = /([\d.]+)([/*\/])([\d.]+)/;

}

The difference is the escape sign “\” before the star sign “*”. Why don’t we need to escape the star sign here?

You are not escaping it in the code you posted. \ is escaping not /


Because it is inside a character class it is matching on the literal star symbol *. It is not the “0 or more times” quantifier.

But both should work (escaped and not escaped) but the test is likely not accounting for that.

Even the / doesn’t need to be escaped in the non-v mode as far as I can tell.

/[/]/.test('/')
true

/[/]/v.test('/')
Uncaught SyntaxError: Invalid regular expression: /[/]/v: Invalid character in character class

/[\/]/v.test('/')
true

1 Like

Thank you a lot! I got it.