UNPKG

buffer-apg-js

Version:

JavaScript APG, an ABNF Parser Generator

136 lines (135 loc) 4.6 kB
<!DOCTYPE html> <html lang="en"> <head> <title>apg-exp</title> <meta charset="utf-8"> <link rel="stylesheet" href="./css/apgexp.css" type="text/css" /> <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>Method: replace()</h1> <p> <kbd>replace()</kbd> replaces the matched pattern or patterns in the string with a replacement string. It works very similarly to JavaScript's <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace">String.replace(regex, string|function)</a>. </p> <h3>Syntax</h3> <pre> var exp = new apgExp(pattern[, flags]); var result = exp.replace(str, replacement);</pre> <h3>Parameters</h3> <p>&bull; str: string: The string to match <kbd>pattern</kbd> in.</p> <p>&bull; replacement: string/function</p> <ul> <li>string: The matched patterns are replaced with this string. The string can contain the following special characters: <table class="apg-left-table"> <tr><th>Pattern</th><th>Inserts</th></tr> <tr><td>$$</td><td>Inserts the character "$".</td></tr> <tr><td>$`</td><td>Inserts the portion of the string preceding the matched pattern.</td></tr> <tr><td>$&</td><td>Inserts the matched pattern.</td></tr> <tr><td>$'</td><td>Inserts the portion of the string following the matched pattern.</td></tr> <tr><td>${name}</td><td>Inserts the sub-string last matched by rule "name" or <kbd>undefined</kbd> if none.</td></tr> </table> </li> <li>&nbsp;</li> <li>function: A function with the prototype: <pre> var func = function(result, exp){ var replacement; /* construct replacement possibly using the values */ /* in result and/or exp */ return replacement; } </pre> </li> </ul> <h3>Return</h3> <p>Returns a copy of <kbd>str</kbd> with the matched pattern or patterns replaced with <kbd>replacement</kbd>.</p> <p>If the <kbd>flags</kbd> argument is absent or empty, only the first match in <kbd>str</kbd> is replaced. If <kbd>flags</kbd> is <kbd>"g"</kbd> or <kbd>"y"</kbd>, <i>all</i> matches in <kbd>str</kbd> are replaced. </p> <h3>Example 1</h3> <p> The <kbd>global</kbd> flag is set, so all matches are replaced. </p> <pre> var pattern, str, exp, result; pattern = 'pattern = A / X\n'; pattern += 'A = "abc"\n'; pattern += 'X = "xyz"\n'; str = "---abc---xyz---ABC---XYZ---"; exp = new apgExp(pattern, "g"); result = exp.replace(str, "555"); console.log(" str: " + str); console.log("result: " + result); /* returns */ str: ---abc---xyz---ABC---XYZ--- result: ---555---555---555---555--- </pre> <h3>Example 2</h3> The <kbd>global</kbd> flag is not set, so only the first match is replaced. The replacement string contains special characters indicating a replacement string of <kbd>"$-|||-$"</kbd> since <kbd>|||</kbd> is the prefix (<kbd>$`</kbd>) to the match. <p> </p> <pre> var pattern, str, exp, result; pattern = 'pattern = A / X\n'; pattern += 'A = "abc"\n'; pattern += 'X = "xyz"\n'; str = "|||abc---xyz---ABC---XYZ---"; exp = new apgExp(pattern, ""); result = exp.replace(str, "$$-$`-$$"); console.log(" str: " + str); console.log("result: " + result); /* returns */ str: |||abc---xyz---ABC---XYZ--- result: |||$-|||-$---xyz---ABC---XYZ--- </pre> <h3>Example 3</h3> <p> The replacement function will examine <kbd>result</kbd> and replace with "555" if rule <kbd>A</kbd> is matched or with "666" if rule <kbd>X</kbd> is matched. </p> <pre> var pattern, str, exp, result; var rfunc = function(result, exp){ var str = "???"; if(result.rules.A){ str = "555"; }else if(result.rules.X){ str = "666"; } return str; } pattern = 'pattern = A / X\n'; pattern += 'A = "abc"\n'; pattern += 'X = "xyz"\n'; str = "---abc---xyz---ABC---XYZ---"; exp = new apgExp(pattern, "g"); result = exp.replace(str, rfunc); console.log(" str: " + str); console.log("result: " + result); /* returns */ str: ---abc---xyz---ABC---XYZ--- result: ---555---666---555---666--- </pre> </div> </div> <div id="footer"></div> </div> </body> </html>