Regular expressions in JS always return false

var pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
str = "411432199911112020";

if(!pattern.test(str)){
    console.log(pattern);
    alert("")
}
//pattern: /(^\d15$)|(^\d18$)|(^\d17(\d|X|x)$)/,
//{}
//[][1]
//dtcms



Jun.24,2022

if the code you write is executed in a JS file or in a browser developer tool, the printed pattern is normal. The problem description says that the curly braces in the pattern printed by console.log are gone, and it is seriously suspected that this code is written in a view engine. Curly braces have a special meaning in this corresponding view engine, such as output variables, {15} directly output constant 15, it seems that the curly braces have disappeared.

Menu