NCName means that it must start with a letter or underscore, and can only contain letters, digits, underscores, hyphens, and periods.I have try to write the regex like this,but I think it may be wrong.
if(/^([_]|[a-zA-Z]+[\w\W])$/.test('abc' )) console.log('match')
Your ^([_]|[a-zA-Z]+[\w\W])$
pattern matches a string that is either equal to _
([_]
) or (|
) is formed of 1+ letters ([a-zA-Z]+
) followed with any char ([\w\W]
). So, it cannot validate the strings of the type you mention.
You may use
/^[a-zA-Z_][\w.-]*$/
See the regex demo and the graph (source) below:
Details
^
- start of string[a-zA-Z_]
- a letter or _
[\w.-]*
- 0 or more letters, digits, underscores, dots or hyphens$
- end of string