Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowed HTML 4.01 id values regex

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 (".").

like image 595
Diosney Avatar asked Feb 02 '13 18:02

Diosney


People also ask

Is allowed in HTML ID?

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).

What are valid values for the ID attribute in HTML?

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.

What is ?: In regex?

'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.)

What does the ID attribute of an HTML element do?

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.


1 Answers

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

like image 180
Anirudha Avatar answered Oct 21 '22 05:10

Anirudha