代码: 全选
h0h6 b9c7 b0c2 c6c5 e3e4 b7b6 h6h4 c9e7 g2e3 b6c6 g3g4 a9b9 g4g5 h7i7 h4h9 g7h9 a0b0 d9e8 b2b7 e7g5 b7i7 h9i7
position fen rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w 1 - 0 1 moves h2e2 h9g7 h0g2 i9h9 i0h0 g6g5代码: 全选
h0h6 b9c7 b0c2 c6c5 e3e4 b7b6 h6h4 c9e7 g2e3 b6c6 g3g4 a9b9 g4g5 h7i7 h4h9 g7h9 a0b0 d9e8 b2b7 e7g5 b7i7 h9i7
position fen rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w 1 - 0 1 moves h2e2 h9g7 h0g2 i9h9 i0h0 g6g5代码: 全选
const text = "position fen rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w 1 - 0 1";
// 1. 如果只是想提取核心的局面和走子方
const match = text.match(/(?:[rnbakcpRNBAKCP1-9]+\/){9}[rnbakcpRNBAKCP1-9]+ [wb]/);
if (match) {
console.log("匹配到的局勢:", match[0]);
}
// 2. 如果要檢查一個字串是否為合法的象棋 FEN (嚴格模式)
function isValidXiangqiFen(str) {
// 這裡加上了結尾的後續參數匹配 (步數等,可選)
const strictRegex = /^(?:[rnbakcpRNBAKCP1-9]+\/){9}[rnbakcpRNBAKCP1-9]+ [wb](?: -| \d+)*$/;
return strictRegex.test(str.trim());
}代码: 全选
const fenRegex = /\b(?:[rnbakcpRNBAKCP1-9]{1,9}\/){9}[rnbakcpRNBAKCP1-9]{1,9} [wb](?:(?: -| \d+){4})?\b/;代码: 全选
movesStr = movesStr.replace(/position fen /, '').replace(fenRegex, '').replace(/ moves /, '');
代码: 全选
// 假設 fenRegex 已經定義好了
const cleanRegex = new RegExp(`position fen | moves |${fenRegex.source}`, 'g');
movesStr = movesStr.replace(cleanRegex, '').trim();正浏览此版面之用户: Bing [Bot] 和 14 访客