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 (".").
In HTML 4, ID values must begin with a letter, which can then be followed only by letters, digits, hyphens, underscores, colons and periods. Just bear in mind that using numbers, punctuation or special characters in the value of an ID may cause trouble in other contexts (e.g., CSS, JavaScript, regex).
Rules for Using the ID AttributeThe ID must start with a letter (a-z or A-Z). All subsequent characters can be letters, numbers (0-9), hyphens (-), underscores (_), colons (:), and periods (.). Each ID must be unique within the document.
'a' (which in this case ?: is doing it is matching with a string but it is excluding whatever comes after it means it will match the string but not whitespace(taking into account match(numbers or strings) not additional things with them.)
The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document). The id attribute is most used to point to a style in a style sheet, and by JavaScript (via the HTML DOM) to manipulate the element with the specific id.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With