{"id":22942,"date":"2024-02-28T15:42:34","date_gmt":"2024-02-28T14:42:34","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/expresiones-regulares-en-javascript-todo-lo-que-necesitas-saber\/"},"modified":"2024-06-03T18:41:01","modified_gmt":"2024-06-03T17:41:01","slug":"expresiones-regulares-en-javascript-todo-lo-que-necesitas-saber","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/regular-expressions-in-javascript-everything-you-need-to-know\/","title":{"rendered":"Regular Expressions in Javascript: Everything you need to know"},"content":{"rendered":"<p>Regular expressions, also known as regex, are a powerful tool for managing patterns in text strings. In the context of programming, especially in Javascript, regular expressions allow us to perform searches, replaces, and validations in an efficient and flexible way.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/regular-expressions-in-javascript-everything-you-need-to-know\/#%C2%BFQue_es_una_expresion_regular\" >What is a regular expression?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/regular-expressions-in-javascript-everything-you-need-to-know\/#Javascript_en_espanol_y_las_expresiones_regulares\" >Javascript in Spanish and regular expressions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/regular-expressions-in-javascript-everything-you-need-to-know\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/regular-expressions-in-javascript-everything-you-need-to-know\/#Preguntas_frecuentes\" >Frequently asked questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/regular-expressions-in-javascript-everything-you-need-to-know\/#1_%C2%BFComo_puedo_aprender_mas_sobre_expresiones_regulares\" >1. How can I learn more about regular expressions?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/regular-expressions-in-javascript-everything-you-need-to-know\/#2_%C2%BFEs_posible_utilizar_expresiones_regulares_en_otros_lenguajes_de_programacion\" >2. Is it possible to use regular expressions in other programming languages?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/regular-expressions-in-javascript-everything-you-need-to-know\/#3_%C2%BFQue_ventajas_ofrecen_las_expresiones_regulares_en_el_desarrollo_web\" >3. What advantages do regular expressions offer in web development?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/regular-expressions-in-javascript-everything-you-need-to-know\/#4_%C2%BFLas_expresiones_regulares_son_case_sensitive_en_Javascript\" >4. Are regular expressions case sensitive in Javascript?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_una_expresion_regular\"><\/span>What is a regular expression?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A regular expression is a sequence of characters that defines a search pattern in a text. It is a combination of literal characters and metacharacters that allows us to perform operations such as finding all matches for a word, validating date formats, or even extracting information from a string.<\/p>\n<p>In Javascript, regular expressions are created using the class <code>RegExp<\/code>. For example, if we want to search for all occurrences of the word &quot;regex&quot; in a text, we can do it as follows:<\/p>\n<pre>\nlet text = &quot;Regular expressions are a very useful tool in programming. Regex allows us to work with search patterns.&quot;; let pattern = \/regex\/g; let results = text.match(pattern); console.log(results);\n<\/pre>\n<p>In this example, we have created a regular expression using the literal <code>\/regex\/g<\/code>. The <code>g<\/code> at the end indicates that all matches will be searched instead of stopping at the first one. The function <code>match()<\/code> returns an array with all the matches found.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Javascript_en_espanol_y_las_expresiones_regulares\"><\/span>Javascript in Spanish and regular expressions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The use of regular expressions in Javascript in Spanish is just as powerful as in any other language. Javascript offers full support for Unicode, so we can work with any type of characters, even accented and special characters.<\/p>\n<p>To use accented characters in regular expressions, we must keep in mind that some characters may have special representations. For example, the letter <code>to<\/code> is represented as <code>u00E1<\/code> and the letter <code>\u00f1<\/code> as <code>u00F1<\/code>. These representations are known as Unicode escape sequences.<\/p>\n<p>Let&#039;s look at an example in which we want to search for all the words that end in &quot;-ci\u00f3n&quot; in a text in Spanish:<\/p>\n<pre>\nlet texto = &quot;Regular expressions in Javascript allow us to do validations in a simple way. In programming, encapsulation is one of the most important characteristics.&quot;; let pattern = \/w+tionb\/g; let results = text.match(pattern); console.log(results);\n<\/pre>\n<p>In this case, we have created a regular expression using the metacharacter <code>w<\/code> to represent any letter or number, followed by the sequence of characters <code>tionb<\/code> to represent any word ending in &quot;-tion&quot;. The <code>b<\/code> is a word limit to make sure it only matches whole words.<\/p>\n<p>Regular expressions in Javascript are widely used in web applications and other projects where text handling is essential. Mastering the use of these tools will give you a great advantage when working with text strings in Javascript.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Regular expressions in Javascript are a valuable tool that allows us to perform searches, replacements and validations efficiently and flexibly. Knowing their syntax and using them correctly will allow us to handle and manipulate text strings effectively.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Preguntas_frecuentes\"><\/span>Frequently asked questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"1_%C2%BFComo_puedo_aprender_mas_sobre_expresiones_regulares\"><\/span>1. How can I learn more about regular expressions?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You can find a lot of resources online, such as tutorials, official documentation, and code examples. You can also explore the NelkoDev website, where you will find articles and practical examples on regular expressions in Javascript.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_%C2%BFEs_posible_utilizar_expresiones_regulares_en_otros_lenguajes_de_programacion\"><\/span>2. Is it possible to use regular expressions in other programming languages?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes, regular expressions are supported by most modern programming languages, such as Python, PHP and C#. Although the syntax and functions may vary slightly, the basic concepts are similar in all of them.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_%C2%BFQue_ventajas_ofrecen_las_expresiones_regulares_en_el_desarrollo_web\"><\/span>3. What advantages do regular expressions offer in web development?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Regular expressions can be used in web development to perform form validation, search and replace text strings, and perform data manipulation and filtering operations. This can save a lot of time and effort in web application development.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_%C2%BFLas_expresiones_regulares_son_case_sensitive_en_Javascript\"><\/span>4. Are regular expressions case sensitive in Javascript?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes, by default regular expressions in Javascript are case sensitive, that is, they are case sensitive. If you want to perform a case-insensitive search, you can use the modifier <code>i<\/code> at the end of the regular expression.<\/p>","protected":false},"excerpt":{"rendered":"<p>Las expresiones regulares, tambi\u00e9n conocidas como regex, son una poderosa herramienta para el manejo de patrones en cadenas de texto. En el contexto de la programaci\u00f3n, especialmente en Javascript, las expresiones regulares nos permiten realizar b\u00fasquedas, reemplazos y validaciones de una manera eficiente y flexible. \u00bfQu\u00e9 es una expresi\u00f3n regular? Una expresi\u00f3n regular es una [&hellip;]<\/p>","protected":false},"author":1,"featured_media":22943,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,30,2206],"tags":[31,205,772,185,477,15,773,75,76],"class_list":["post-22942","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-javascript","category-php","tag-que","tag-blog","tag-expresiones","tag-javascript","tag-necesitas","tag-php","tag-regulares","tag-saber","tag-todo"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/22942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=22942"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/22942\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/22943"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=22942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=22942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=22942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}