{"id":9890,"date":"2015-04-19T19:24:34","date_gmt":"2015-04-19T19:24:34","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=9890"},"modified":"2015-04-19T19:30:52","modified_gmt":"2015-04-19T19:30:52","slug":"javascript-regex-part-ii","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/javascript-regex-part-ii\/","title":{"rendered":"Javascript:  Regex part II"},"content":{"rendered":"<h1>The tutor continues about regex in the context of Javascript.<\/h1>\n<p>In <a href=\"?p=9877\">yesterday&#8217;s post<\/a> I brought up regular expressions with an interactive example. Today, some more depth:<\/p>\n<p>Let&#8217;s imagine an input string that means an amount of money. Likely, the amount includes a currency symbol; perhaps it&#8217;s<\/p>\n<p><span style=\"font-family: monospace;\">$1 117.89<\/span><\/p>\n<p>or<\/p>\n<p><span style=\"font-family:monospace\">\u20ac1 117.89<\/span><\/p>\n<p>To Javascript, the presence of a currency symbol means the string is not a number.  The same goes for the space between the digits.  Therefore, if calculation is to be done on the number, it must be amended to<br \/>\n<span style=\"font-family:monospace\"><br \/>\n1117.89<br \/>\n<\/span><\/p>\n<p>Let&#8217;s imagine the input is contained in the string <span style=\"font-family:monospace\">catch0<\/span>, like so:<br \/>\n<span style=\"font-family:monospace\"><br \/>\nvar catch0=&#8221;\u20ac1 117.89&#8243;;<br \/>\n<\/span><br \/>\nEverything but the digits and decimal point needs to be removed.  The <span style=\"font-family:monospace\">replace()<\/span> function can be used, as follows:<br \/>\n<span style=\"font-family:monospace\"><br \/>\nvar catch1=catch0.replace(\/[^0-9.]\/g,&#8221;&#8221;);<br \/>\n<\/span><\/p>\n<p>The <span style=\"font-family:monospace\">replace()<\/span> function must be applied to a string.  It can use the following syntax:<br \/>\n<span style=\"font-family:monospace\"><br \/>\nthestring.replace(\/to_be_replaced\/,&#8221;replacement&#8221;);<\/p>\n<p>For instance, let&#8217;s imagine the string<br \/>\n<span style=\"font-family:monospace\"><br \/>\nvar mailstring=&#8221;mailatsomewhere.there&#8221;;<br \/>\n<\/span><\/p>\n<p>Further, let&#8217;s imagine that we want to replace <span style=\"font-family:monospace\">at<\/span> with <span style=\"font-family:monospace\">@<\/span>.  We could do so as follows:<br \/>\n<span style=\"font-family:monospace\"><br \/>\nvar numailstring=mailstring.replace(\/at\/, &#8220;@&#8221;);<br \/>\n<\/span><\/p>\n<p>Now, the value of <span style=\"font-family:monospace\">numailstring<\/span> is <span style=\"font-family:monospace\">mail@somewhere.there<\/span><\/p>\n<p>This doesn&#8217;t quite explain the workings of the example from last post, but it&#8217;s on the way.  I&#8217;ll be continuing &#8230;:)<\/p>\n<p>Source:<\/p>\n<p><a href=\"http:\/\/www.w3schools.com\/jsref\/jsref_replace.asp\">w3schools.com<\/a><\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tutor continues about regex in the context of Javascript. In yesterday&#8217;s post I brought up regular expressions with an interactive example. Today, some more depth: Let&#8217;s imagine an input string that means an amount of money. Likely, the amount &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/javascript-regex-part-ii\/\"> <span class=\"screen-reader-text\">Javascript:  Regex part II<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[205,713,711,712],"class_list":["post-9890","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-javascript","tag-javascript-editing-input","tag-javascript-regex","tag-javascript-replace-function"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/9890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=9890"}],"version-history":[{"count":35,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/9890\/revisions"}],"predecessor-version":[{"id":9925,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/9890\/revisions\/9925"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=9890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=9890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=9890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}