"hello small world and blue sky".match(/(\S+)\s+(\S+)/);
> x = "I have 2 numbers: 53147" > pats = [ /(.*)(\d*)/, /(.*)(\d+)/, /(.*?)(\d*)/, /(.*?)(\d+)/, /(.*)(\d+)$/, /(.*?)(\d+)$/, /(.*)\b(\d+)$/, /(.*\D)(\d+)$/ ]Es decir, compute la salida de:
pats.map( function(r) { return r.exec(x).slice(1); })
> a = "hola juan" => "hola juan" > a.match(/(?:hola )*(juan)/)
"hello world"
y en las que las comillas puedan aparecer escapadas como en "Hello \"Jane\" and Jakes"
2.34
, -5.2e-1
y 0.9e3
m[0]
?
m = 'main() /* 1c */ { /* 2c */ return; /* 3c */ }'.match(new RegExp('/\\*.*\\*/'))¿Por qué?
\
en la expresión regular new RegExp('/\\*.*\\*/')
de la pregunta anterior 6?
> 'ab,cd,4,3, de, fg'.replace(/,/, ', ') => "ab, cd, 4, 3, de, fg"pero se quiere que la sustitución no tenga lugar si la coma esta incrustada entre dos dígitos. Además se pide que si hay ya un espacio después de la coma, no se duplique
// ...
/* ... */
> re = __________ > str = "John Smith" 'John Smith' > newstr = str.replace(re, "______") 'Smith, John'
> re = /d(b+)(d)/ig /d(b+)(d)/gi > z = "dBdxdbbdzdbd" 'dBdxdbbdzdbd' > result = re.exec(z) [ ______, _____, ______, index: __, input: 'dBdxdbbdzdbd' ] > re.lastIndex ______ > result = re.exec(z) [ ______, _____, ______, index: __, input: 'dBdxdbbdzdbd' ] > re.lastIndex ______ > result = re.exec(z) [ ______, _____, ______, index: __, input: 'dBdxdbbdzdbd' ] > re.lastIndex ______ > result = re.exec(z) _____
r
para que produzca el resultado final:
> x = "hello" > r = /l(___)/ > z = r.exec(x) [ 'l', index: 3, input: 'hello' ]
> z = "dBdDBBD" > re = /d(b+)(d)/ig > re.lastIndex = ________ > result = re.exec(z) [ 'DBBD', 'BB', 'D', index: 3, input: 'dBdDBBD' ]
> RegExp.prototype.bexec = function(str) { ... var i = this.lastIndex; ... var m = this.exec(str); ... if (m && m.index == i) return m; ... return null; ... } [Function]
> re = /d(b+)(d)/ig /d(b+)(d)/gi > z = "dBdXXXXDBBD" 'dBdXXXXDBBD' > re.lastIndex = 3 > re.bexec(z) _____________________________________________________ > re.lastIndex = 7 > re.bexec(z) _____________________________________________________
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=_____"> <title>JavaScript Temperature Converter</title> <link ____="global.css" ___="stylesheet" ____="text/css"> <script type="_______________" src="temperature.js"></script> </head> <____> <h1>Temperature Converter</h1> <table> <tr> <th>Enter Temperature (examples: 32F, 45C, -2.5f):</th> <td><input id="________" ________="calculate();"></td> </tr> <tr> <th>Converted Temperature:</th> <td><span class="output" id="_________"></span></td> </tr> </table> </____> </html>
temperature.js
:
"use strict"; // Use ECMAScript 5 strict mode in browsers that support it function calculate() { var result; var original = document.getElementById("________"); var temp = original.value; var regexp = /_______________________________/; var m = temp.match(______); if (m) { var num = ____; // paréntesis correspondiente var type = ____; num = parseFloat(num); if (type == 'c' || type == 'C') { result = (num * 9/5)+32; result = ______________________________ // 1 sólo decimal y el tipo } else { result = (num - 32)*5/9; result = ____________________________ // 1 sólo decimal y el tipo } converted._________ = result; // Insertar "result" en la página } else { converted._________ = "ERROR! Try something like '-4.2C' instead"; } }
autofocus
?
<td><textarea autofocus cols = "80" rows = "5" id="original"></textarea></td>
tr:nth-child(odd) { background-color:#eee; } tr:nth-child(even) { background-color:#00FF66; }
window
en un programa JavaScript que se ejecuta en un navegador?
if (window.localStorage) localStorage.original = temp;
window.onload = function() { // If the browser supports localStorage and we have some stored data if (window.localStorage && localStorage.original) { document.getElementById("original").value = localStorage.original; } };
.hidden { display: ____; } .unhidden { display: _____; }
var entityMap = { "&": "&___;", "<": "&__;", ">": "&__;", '"': '"', "'": ''', "/": '/' }; function escapeHtml(string) { return String(string).replace(/_________/g, function (s) { return ____________; });
replace
.
¿Que argumentos recibe?
> "bb".match(/b|bb/) > "bb".match(/bb|b/)
index.html
.
Comenta que hace el tag <input>
.
Comenta que hace el tag <pre>
.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>INI files</title> <link href="global.css" rel="__________" type="text/css"> <script type="_______________" src="underscore.js"></script> <script type="_______________" src="jquery.js"></script> <script type="_______________" src="______"></script> </head> <body> <h1>INI files</h1> <input type="file" id="_________" /> <div id="out" class="hidden"> <table> <tr><th>Original</th><th>Tokens</th></tr> <tr> <td> <pre class="input" id="____________"></pre> </td> <td> <pre class="output" id="___________"></pre> </td> </tr> </table> </div> </body> </html>
ini.js
conteniendo el JavaScript.
.ini
le puede ayudar
a recordar la parte de las expresiones regulares
; last modified 1 April 2001 by John Doe [owner] name=John Doe organization=Acme Widgets Inc.
"use ______"; // Use ECMAScript 5 strict mode in browsers that support it $(document)._____(function() { $("#fileinput").______(calculate); }); function calculate(evt) { var f = evt.target.files[0]; if (f) { var r = new __________(); r.onload = function(e) { var contents = e.target.______; var tokens = lexer(contents); var pretty = tokensToString(tokens); out.className = 'unhidden'; initialinput._________ = contents; finaloutput._________ = pretty; } r.__________(f); // Leer como texto } else { alert("Failed to load file"); } } var temp = '<li> <span class = "<%= ______ %>"> <%= _ %> </span>\n'; function tokensToString(tokens) { var r = ''; for(var i in tokens) { var t = tokens[i]; var s = JSON.stringify(t, undefined, 2); //______________________________ s = _.template(temp, {t: t, s: s}); r += s; } return '<ol>\n'+r+'</ol>'; } function lexer(input) { var blanks = /^___/; var iniheader = /^________________/; var comments = /^________/; var nameEqualValue = /^________________________/; var any = /^_______/; var out = []; var m = null; while (input != '') { if (m = blanks.____(input)) { input = input.substr(m.index+___________); out.push({ type : ________, match: _ }); } else if (m = iniheader.exec(input)) { input = input.substr(___________________); _______________________________________ // avanzemos en input } else if (m = comments.exec(input)) { input = input.substr(___________________); _________________________________________ } else if (m = nameEqualValue.exec(input)) { input = input.substr(___________________); _______________________________________________ } else if (m = any.exec(input)) { _______________________________________ input = ''; } else { alert("Fatal Error!"+substr(input,0,20)); input = ''; } } return out; }
Casiano Rodríguez León