问题
Can you help me to build a regex that matches a valid W3C HTML 4.01 id
value?
According with W3C specs:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
回答1:
You can use this regex
^[a-zA-Z][\w:.-]*$
^
depicts the start of string
[a-zA-Z]
matches an uppercase or lowercase letter
*
matches the preceding character 1 to many times
\w
is similar to [a-zA-Z\d_]
$
is the end of string
来源:https://stackoverflow.com/questions/14664860/allowed-html-4-01-id-values-regex