const regex = /^
(?# Piece Placement Section)(?<PiecePlacement>((?<RankItem>[pnbrqkPNBRQK1-8]{1,8})\/?){8})
(?# Section Separator)\s+
(?# Side To Move Section) (?<SideToMove>b|w)
(?# Section Separator)\s+
(?# Castling Ability) (?<Castling>-|K?Q?k?q)
(?# Section Separator)\s+
(?# En passant) (?<EnPassant>-|[a-h][3-6])
(?# Section Separator)\s+
(?# Half Move Clock) (?<HalfMoveClock>\d+)
(?# Section Separator)\s+
(?# Full Move Number) (?<FullMoveNumber>\d+)
\s*
$/;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^
(?# Piece Placement Section)(?<PiecePlacement>((?<RankItem>[pnbrqkPNBRQK1-8]{1,8})\\\/?){8})
(?# Section Separator)\\s+
(?# Side To Move Section) (?<SideToMove>b|w)
(?# Section Separator)\\s+
(?# Castling Ability) (?<Castling>-|K?Q?k?q)
(?# Section Separator)\\s+
(?# En passant) (?<EnPassant>-|[a-h][3-6])
(?# Section Separator)\\s+
(?# Half Move Clock) (?<HalfMoveClock>\\d+)
(?# Section Separator)\\s+
(?# Full Move Number) (?<FullMoveNumber>\\d+)
\\s*
$', '')
const str = `rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
if ((m = regex.exec(str)) !== null) {
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions