{"id":147,"date":"2022-11-24T19:25:44","date_gmt":"2022-11-24T19:25:44","guid":{"rendered":"http:\/\/javigomez.org\/?p=147"},"modified":"2025-09-29T20:34:20","modified_gmt":"2025-09-29T20:34:20","slug":"parsear-un-texto-y-sustituir-una-cadena","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2022\/11\/24\/parsear-un-texto-y-sustituir-una-cadena\/","title":{"rendered":"Parsear un texto y sustituir una cadena"},"content":{"rendered":"\n<p>En este c\u00f3digo se analiza (parsea) un texto buscando una cadena que se solicita por pantalla y se sustituye por &#8220;XXXXX&#8221; (este valor puede ser cualquiera). <\/p>\n\n\n\n<p>En el c\u00f3digo se puede ver que hay dos botones para sustituir. El primero se realiza sin expresiones regulares, solo utilizando la funci\u00f3n <strong>String.replace(origen,destino)<\/strong>. Esta funci\u00f3n solo reemplaza la primera que encuentra, con lo que es necesario meterse en un bucle hasta que <strong>String.indexOf(origen)<\/strong> sea diferente de -1. (Tanto <strong>origen <\/strong>como <strong>destino <\/strong>son variables String)<\/p>\n\n\n\n<p>En una segunda funci\u00f3n se hace con una expresi\u00f3n regular. Para ello se utiliza la funci\u00f3n <strong>exp=RegExp(origen,&#8221;g&#8221;)<\/strong>. Esto crea una expresi\u00f3n regular del tipo \/valor_de_origen\/g. A continuaci\u00f3n se puede hacer un <strong>String.replace(exp,&#8221;XXXXX&#8221;)<\/strong>;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"http:\/\/javigomez.org\/wp-content\/uploads\/2022\/11\/image-29-2-13-1.png\" alt=\"\" class=\"wp-image-148\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n\n&lt;head>\n      &lt;meta charset=\"UTF-8\">\n      &lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n      &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n      &lt;title>4.12&lt;\/title>\n&lt;\/head>\n\n&lt;body>\n      &lt;h1>4.12 Parsea un texto&lt;\/h1>\n      &lt;p>Introduce el texto que se va a cambiar por XXXXX: &lt;\/p>\n      &lt;textarea id=\"texto\">&lt;\/textarea>\n\n      &lt;p>\n            Cadena a buscar: &lt;input type=\"text\" placeholder=\"Texto a sustituir por XXXXX\" id=\"buscado\">\n\n            &lt;input type=\"button\" value=\"Sustituir sin RegExp\" onclick=\"buscar2()\">\n            &lt;input type=\"button\" value=\"Sustituir con RegExp\" onclick=\"buscar()\">\n      &lt;\/p>\n\n      &lt;p id=\"resultado\">&lt;\/p>\n\n      &lt;script>\n            \/\/ realiza la b\u00fasqueda sin utilizar expresiones regulares\n            function buscar2() {\n                  var sCad = document.getElementById(\"texto\").value;\n                  var sBuscado = document.getElementById(\"buscado\").value;\n\n             \/*     while (sCad.indexOf(sBuscado) != -1) {\n                        var sCad = sCad.replace(sBuscado, \"XXXXX\");\n                  }\n*\/\n                  sCad=sCad.replaceAll(sBuscado,\"XXXXX\");\n\n                  document.getElementById(\"resultado\").innerHTML = sCad;\n            }\n            \/\/ realiza la b\u00fasqueda y sustituci\u00f3n con expresiones regulares\n            function buscar() {\n                  var sCad = document.getElementById(\"texto\").value;\n                  var sBuscado = document.getElementById(\"buscado\").value;\n\n                  var exp = new RegExp(sBuscado, \"g\"); \/\/crea la expresi\u00f3n regular  - \/cadena\/g - \n                  var sFinal = sCad.replace(exp, \"XXXXX\");\n\n                  document.getElementById(\"resultado\").innerHTML = sFinal;\n            }\n      &lt;\/script>\n&lt;\/body>\n\n&lt;\/html><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este c\u00f3digo se analiza (parsea) un texto buscando una cadena que se solicita por pantalla y se sustituye por &#8220;XXXXX&#8221; (este valor puede ser &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[193,204,260,261,285,289,318],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-indexof","tag-javascript","tag-parsear","tag-parser","tag-regexp","tag-replace","tag-sustituir"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":977,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions\/977"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}