@@ -311,12 +311,22 @@ chess.load('4r3/8/X12XPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45');
311
311
### .load_pgn(pgn, [ options ] )
312
312
Load the moves of a game stored in
313
313
[ Portable Game Notation] ( http://en.wikipedia.org/wiki/Portable_Game_Notation ) .
314
- Options is an optional parameter that may contain a ` newline_char ` which is a
315
- string representation of a RegExp (and should not be pre-escaped) and defaults
316
- to ` \r?\n ` ). Options may also contain a ` sloppy ` flag which allows chess.js
317
- to parse moves in various non-standard notations (see ` .move ` documentation
318
- for more information). Returns true if the PGN was parsed successfully,
319
- otherwise false.
314
+ ` pgn ` should be a string. Options is an optional ` object ` which may contain
315
+ a string ` newline_char ` and a boolean ` sloppy ` .
316
+
317
+ The ` newline_char ` is a string representation of a valid RegExp fragment and is
318
+ used to process the PGN. It defaults to ` \r?\n ` . Special characters
319
+ should not be pre-escaped, but any literal special characters should be escaped
320
+ as is normal for a RegExp. Keep in mind that backslashes in JavaScript strings
321
+ must themselves be escaped (see ` sloppy_pgn ` example below). Avoid using
322
+ a ` newline_char ` that may occur elsewhere in a PGN, such as ` . ` or ` x ` , as this
323
+ will result in unexpected behavior.
324
+
325
+ The ` sloppy ` flag is a boolean that permits chess.js to parse moves in
326
+ non-standard notations. See ` .move ` documentation for more information about
327
+ non-SAN notations.
328
+
329
+ The method will return ` true ` if the PGN was parsed successfully, otherwise ` false ` .
320
330
321
331
``` js
322
332
var chess = new Chess ();
@@ -342,10 +352,10 @@ pgn = ['[Event "Casual Game"]',
342
352
chess .load_pgn (pgn .join (' \n ' ));
343
353
// -> true
344
354
345
- chess .fen ()
355
+ chess .fen ();
346
356
// -> 1r3kr1/pbpBBp1p/1b3P2/8/8/2P2q2/P4PPP/3R2K1 b - - 0 24
347
357
348
- chess .ascii ()
358
+ chess .ascii ();
349
359
// -> ' +------------------------+
350
360
// 8 | . r . . . k r . |
351
361
// 7 | p b p B B p . p |
@@ -357,6 +367,39 @@ chess.ascii()
357
367
// 1 | . . . R . . K . |
358
368
// +------------------------+
359
369
// a b c d e f g h'
370
+
371
+
372
+ // Parse non-standard move formats and unusual line separators
373
+ var sloppy_pgn = [' [Event "Wijk aan Zee (Netherlands)"]' ,
374
+ ' [Date "1971.01.26"]' ,
375
+ ' [Result "1-0"]' ,
376
+ ' [White "Tigran Vartanovich Petrosian"]' ,
377
+ ' [Black "Hans Ree"]' ,
378
+ ' [ECO "A29"]' ,
379
+ ' ' ,
380
+ ' 1. Pc2c4 Pe7e5' , // non-standard
381
+ ' 2. Nc3 Nf6' ,
382
+ ' 3. Nf3 Nc6' ,
383
+ ' 4. g2g3 Bb4' , // non-standard
384
+ ' 5. Nd5 Nxd5' ,
385
+ ' 6. c4xd5 e5-e4' , // non-standard
386
+ ' 7. dxc6 exf3' ,
387
+ ' 8. Qb3 1-0'
388
+ ].join (' |' );
389
+
390
+ var options = {
391
+ newline_char: ' \\ |' , // Literal '|' character escaped
392
+ sloppy: true
393
+ };
394
+
395
+ chess .load_pgn (sloppy_pgn);
396
+ // -> false
397
+
398
+ chess .load_pgn (sloppy_pgn, options);
399
+ // -> true
400
+
401
+ chess .fen ();
402
+ // -> 'r1bqk2r/pppp1ppp/2P5/8/1b6/1Q3pP1/PP1PPP1P/R1B1KB1R b KQkq - 1 8'
360
403
```
361
404
362
405
### .move(move, [ options ] )
0 commit comments