{"id":600,"date":"2013-01-01T17:32:12","date_gmt":"2013-01-01T17:32:12","guid":{"rendered":"https:\/\/greladesign.co\/blog\/?p=600"},"modified":"2013-01-02T09:25:51","modified_gmt":"2013-01-02T09:25:51","slug":"regex-search-and-replace","status":"publish","type":"post","link":"https:\/\/greladesign.co\/blog\/2013\/01\/01\/regex-search-and-replace\/","title":{"rendered":"RegEx &#8211; search and replace"},"content":{"rendered":"<p>The problem is to search bit of text and replace any occurance of the searched string with something new, with <code>RegEx<\/code> class and built-in <code>replace<\/code> function of <code>String<\/code> class it is very easy.<br \/>\n<!--more--><\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\r\nvar query:String = &quot;volutpat&quot;;\r\nvar text:Stringt = &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer VOLUTPAT faucibus lectus in ultrices. Nunc semper interdum eros luctus rutrum. Praesent ipsum nibh, laoreet quis placerat ac, gravida ac libero. Duis risus nisi, malesuada nec varius quis, laoreet eu odio. Nullam nec porttitor lectus. Fusce tempor mauris eu ante imperdiet sodales. Phasellus suscipit risus id augue volutpat ut commodo lectus accumsan. Quisque dapibus dui at eros hendrerit consectetur. Quisque vel augue vel elit rutrum volutpat.&quot;;\r\nvar replace:String = &quot;REPLACED&quot;;\r\nvar result:String;\r\n\r\nresult = text.replace(new RegExp(query, &quot;g&quot;), replace);\r\n\r\ntrace(result);\r\n<\/pre>\n<p>The <code>result<\/code> will no contain string from <code>text<\/code> variable with each occurance of <code>query<\/code> changed to value of <code>replace<\/code>. You will notice that upper cased searched word wasn&#8217;t changed &#8211; because the search was case sensitive. To make it case insensitive we need to add <code>i<\/code> to the options in the RegEx constructor (existing &#8220;g&#8221; is for global &#8211; so ALL occurances are found not just first).<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nresult = text.replace(new RegExp(query, &quot;gi&quot;), replace);\r\n<\/pre>\n<p>Second argument of the replace method can be a function, this replacement function is very convenient if we want to do some operations before replacing, e.g. here you could change the letter-case.<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\r\nfunction replaceFunction(...args):String\r\n{\r\n    var match:String = args[0];\r\n    var index:int = args[args.length - 2];\r\n    var fullText:String = args[args.length - 1];\r\n\r\n    \/\/parenthical groups\r\n    \/\/var pl:int = args.length - 3;\r\n    \/\/for (var i:int = 0; i &lt; pl; i++) \r\n    \/\/{\r\n    \/\/    trace(&quot;parenthical group $&quot; + (i + 1) +&quot;=&quot; + args[i + 1]);\r\n    \/\/}\r\n    \/\/\r\n\r\n    return match.toUpperCase();\r\n}\r\n\r\nresult = text.replace(new RegExp(query, &quot;gi&quot;), replaceFunction);\r\n\r\n<\/pre>\n<p>As a note to the replacement function, the documentation doesn&#8217;t specify that this function should have arguments defined and in result it may run into ArgumentError, the solution is described here: <a href=\"https:\/\/greladesign.co\/blog\/2011\/12\/08\/the-strings-replace-method-and-argumenterror-error-1063-argument-count-mismatch-error\/\" title=\"The String\u2019s replace method and ArgumentError: Error #1063: Argument count mismatch error\">The String\u2019s replace method and ArgumentError: Error #1063: Argument count mismatch error<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The problem is to search bit of text and replace any occurance of the searched string with something new, with RegEx class and built-in replace function of String class it is very easy.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":""},"categories":[17,9],"tags":[12,214,34,215],"_links":{"self":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/600"}],"collection":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/comments?post=600"}],"version-history":[{"count":7,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/600\/revisions"}],"predecessor-version":[{"id":607,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/600\/revisions\/607"}],"wp:attachment":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/media?parent=600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/categories?post=600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/tags?post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}