What is xml-char-classes?
The xml-char-classes npm package provides regular expressions for matching XML character classes. It helps in validating and working with XML data by ensuring that characters conform to XML specifications.
What are xml-char-classes's main functionalities?
Match XML Name Start Characters
This feature provides a regular expression to match valid XML name start characters. XML names must start with a letter or underscore, and this regex helps ensure that.
const xmlCharClasses = require('xml-char-classes');
const regex = new RegExp(`^[${xmlCharClasses.nameStartChar.source}]`);
console.log(regex.test('A')); // true
console.log(regex.test('1')); // false
Match XML Name Characters
This feature provides a regular expression to match valid XML name characters. XML names can include letters, digits, hyphens, underscores, and periods, and this regex helps ensure that.
const xmlCharClasses = require('xml-char-classes');
const regex = new RegExp(`^[${xmlCharClasses.nameChar.source}]+$`);
console.log(regex.test('A1-._')); // true
console.log(regex.test('A1-._ ')); // false
Match XML Char
This feature provides a regular expression to match valid XML characters. It ensures that the characters conform to the XML specification for valid characters.
const xmlCharClasses = require('xml-char-classes');
const regex = new RegExp(`^[${xmlCharClasses.char.source}]+$`);
console.log(regex.test('A1-._ ')); // true
console.log(regex.test('\u0000')); // false
Other packages similar to xml-char-classes
xml2js
The xml2js package is used for parsing XML to JavaScript objects and vice versa. While it does not focus on character classes, it provides comprehensive XML parsing and building functionalities.
fast-xml-parser
The fast-xml-parser package is a fast and lightweight XML parser. It provides similar XML parsing capabilities but does not specifically focus on character classes like xml-char-classes.
sax
The sax package is a streaming XML parser. It provides low-level parsing capabilities and can be used to validate XML data, but it does not specifically provide regular expressions for XML character classes.
xml-char-classes
Regular expressions for matching against the XML Character Classes
Install
$ npm install xml-char-classes
Usage
import xmlCharacterClasses from 'xml-char-classes';
console.log(xmlCharacterClasses.digit);
xmlCharacterClasses.letter.test('unicorn');
xmlCharacterClasses.letter.test('#');
API
xmlCharClasses
Contains the following regular expressions for matching the different character classes in the XML spec.
letter
baseChar
ideographic
combiningChar
digit
extender