UNPKG

buffer-apg-js

Version:

JavaScript APG, an ABNF Parser Generator

83 lines (82 loc) 2.54 kB
<!DOCTYPE html> <html lang="en"> <head> <title>apg-exp</title> <meta charset="utf-8"> <link rel="stylesheet" href="./css/BrightSide.css" type="text/css" /> <script type="text/javascript" src="./import.js"></script> </head> <body> <div id="wrap"> <div id="header"></div> <div id="content-wrap"> <img src="./images/PointLobosCropped.jpg" width="820" height="120" alt="headerphoto" class="no-border" /> <div id="sidebar"></div> <div id="main-2col"> <!-- page content goes here --> <h1>Sticky Mode</h1> <h3>Syntax</h3> <pre> var exp = new apgExp(pattern, "g"); </pre> <p> The pattern match attempt is made at the index, <kbd><a href="./lastIndex.html">exp.lastIndex</a></kbd>. The user can set <kbd>exp.lastIndex</kbd> to any value prior to the match attempt. If a match is not found <kbd>exp.lastIndex</kbd> the attempt fails and returns <kbd>null</kbd>. In sticky mode if a match is found, <kbd>exp.lastIndex</kbd> is incremented by the length of the matched pattern, or one if the length is zero. (Some patterns allow matches to empty strings. In this case <kbd>exp.lastIndex</kbd> is incremented by one to prevent infinite loops and to allow the sticky search to continue.) This allows for an iteration over all <i>consecutive</i> matched patterns in the <kbd>input</kbd> string. When the match fails, <kbd>exp.lastIndex</kbd> is then reset to zero. </p> <h3>Example 1</h3> <pre> var pattern, str, exp, result; pattern = 'pattern = "abc"\n'; exp = new apgExp(pattern, "y"); str = "---abc---ABC---"; while(result = exp.exec(str)){ console.log("found: " + result[0] + " :at: " + result.index); } console.log("result: null"); /* returns */ result: null </pre> <h3>Example 2</h3> <pre> var pattern, str, exp, result; pattern = 'pattern = "abc"\n'; exp = new apgExp(pattern, "y"); exp.lastIndex = 3; str = "---abc---ABC---"; while(result = exp.exec(str)){ console.log("found: " + result[0] + " :at: " + result.index); } console.log("result: null"); /* returns */ found: abc :at: 3 result: null </pre> <h3>Example 3</h3> <pre> var pattern, str, exp, result; pattern = 'pattern = "abc"\n'; exp = new apgExp(pattern, "y"); str = "abcABCabc"; while(result = exp.exec(str)){ console.log("found: " + result[0] + " :at: " + result.index); } console.log("result: null"); /* returns */ found: abc :at: 0 found: ABC :at: 3 found: abc :at: 6 result: null </pre> </div> </div> <div id="footer"></div> </div> </body> </html>