Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegExp /\c/ in JavaScript

Tags:

javascript

RegExp /\c/ doesn't trigger any syntax error.

console.log(/\c/)

The question is why it's not a syntax error. Since the language spec, I'm guessing PatternDisjunctionAlternativeTermAtom\ AtomEscapeCharacterEscapeIdentityEscape, then it arrives at SourceCharacter but not c and it doesn't match by the condition but not c.

https://www.ecma-international.org/ecma-262/8.0/#sec-regular-expressions-patterns

I wonder if I'm wrong.

like image 552
mysticatea Avatar asked Feb 09 '18 04:02

mysticatea


1 Answers

I found it.

The \c doesn't match to \ AtomEscape alternative. This was correct. So the \ letter matches to ExtendedPatternCharacter and the c letter matches to ExtendedPatternCharacter individually.

/^\x$/.test("x") //→ true
/^\c$/.test("c") //→ false
/^\c$/.test("\\c") //→ true
like image 138
mysticatea Avatar answered Oct 21 '22 00:10

mysticatea