Reputation: 477
Im trying to remove the the comma and numericals after, but are unsure how. I know how to target it if i knew the characters, like this:
$("em.price.product-card-price").text(function(index, text) {
return text.replace(',62', '');
});
But since the number is dynamic that wont work.
So what is the best way to do this?
<em class="price product-card-price">1090,62€</em>
Upvotes: 0
Views: 140
Reputation: 830
$("em.price.product-card-price").text(function(index, text) {
var value = text.split(',')[0];
var currency = text.match(/((?:AED|AFN|ALL|AMD|ANG|AOA|ARS|AUD|AWG|AZN|BAM|BBD|BDT|BGN|BHD|BIF|BMD|BND|BOB|BRL|BSD|BTN|BWP|BYR|BZD|CAD|CDF|CHF|CLP|CNY|COP|CRC|CUP|CVE|CZK|DJF|DKK|DOP|DZD|EGP|ERN|ETB|EUR|FJD|FKP|GBP|GEL|GHS|GIP|GMD|GNF|GTQ|GYD|HKD|HNL|HRK|HTG|HUF|IDR|ILS|INR|IQD|IRR|ISK|JMD|JOD|JPY|KES|KGS|KHR|KMF|KPW|KRW|KWD|KYD|KZT|LAK|LBP|LKR|LRD|LTL|LYD|MAD|MDL|MGA|MKD|MMK|MNT|MOP|MRO|MUR|MWK|MXN|MYR|MZN|NAD|NGN|NIO|NOK|NPR|NZD|OMR|PAB|PEN|PGK|PHP|PKR|PLN|PYG|QAR|RON|RSD|RUB|RWF|SAR|SBD|SCR|SDG|SEK|SGD|SHP|SLL|SOS|SRD|SSP|STD|SYP|SZL|THB|TJS|TMT|TND|TOP|TRY|TTD|TWD|TZS|UAH|UGX|USD|UYU|UZS|VEF|VND|VUV|WST|XAF|XCD|XOF|XPF|YER|ZAR|ZMW)|(?:원|RMB|руб|руб\.|Lt|ر\.ق\.|р\.|د\.ب\.|TSh|din\.|Rp|ر|WS\$|Rs|T\$|S\/\.|SR|Bs\.|NOK|CF|Fdj|£|¤|¥|SEK|Br|Bs|MTn|د\.أ\.|ден|den|RUB|أ\.م\.|лв\.|नेरू|DA|zł|Nfk|дин|дин\.|din|din\.|ر\.ي\.|US\$|Ksh|د\.ت\.|CFA|DT|MAD|B\/\.|NT\$|FCFA|soʻm|UM|Db|CVE|man\.|EC\$|PLN|රු\.|ر\.س\.|ج\.م\.|ر\.ع\.|¥|CA\$|ALL|Kč|د\.إ\.|դր\.|៛|د\.ك\.|ل\.ل\.|Afl\.|сом|LEI|kn|kr|kr\.|KM|Ft\.|VT|FC|ف\.ج\.ق\.|Fr\.|SFr\.|FCFP|m\.|ریال|FG|ج\.س\.|د\.ج\.|КМ|R\$|Lekë|৳|د\.ل\.|ل\.س\.|Nu\.|ман\.|₡|฿|₦|₩|ብር|₪|₫|€|₭|₮|₱|\$|S\$|₲|GEL|TRY|₴|₸|₹|₺|₽|Kz|LS|RF|MOP\$|GH₵|D|E|د\.ع\.|FBu|G|د\.م\.|Ft|K|RM|L|USh|P|Q|Le|R|S|Rs\.|NAf\.|DKK|؋|Ar|C\$|MK))/gm);
return currency + value;
});
http://jsbin.com/boxorejijo/edit?js,console,output
First let me apologies for the length of the regex, essentially it matches the currency then splits the text by the comma and returns them added together.
var text = "1090,62€";
var value = text.split(',')[0];
var currency = text.match(/((?:AED|AFN|ALL|AMD|ANG|AOA|ARS|AUD|AWG|AZN|BAM|BBD|BDT|BGN|BHD|BIF|BMD|BND|BOB|BRL|BSD|BTN|BWP|BYR|BZD|CAD|CDF|CHF|CLP|CNY|COP|CRC|CUP|CVE|CZK|DJF|DKK|DOP|DZD|EGP|ERN|ETB|EUR|FJD|FKP|GBP|GEL|GHS|GIP|GMD|GNF|GTQ|GYD|HKD|HNL|HRK|HTG|HUF|IDR|ILS|INR|IQD|IRR|ISK|JMD|JOD|JPY|KES|KGS|KHR|KMF|KPW|KRW|KWD|KYD|KZT|LAK|LBP|LKR|LRD|LTL|LYD|MAD|MDL|MGA|MKD|MMK|MNT|MOP|MRO|MUR|MWK|MXN|MYR|MZN|NAD|NGN|NIO|NOK|NPR|NZD|OMR|PAB|PEN|PGK|PHP|PKR|PLN|PYG|QAR|RON|RSD|RUB|RWF|SAR|SBD|SCR|SDG|SEK|SGD|SHP|SLL|SOS|SRD|SSP|STD|SYP|SZL|THB|TJS|TMT|TND|TOP|TRY|TTD|TWD|TZS|UAH|UGX|USD|UYU|UZS|VEF|VND|VUV|WST|XAF|XCD|XOF|XPF|YER|ZAR|ZMW)|(?:원|RMB|руб|руб\.|Lt|ر\.ق\.|р\.|د\.ب\.|TSh|din\.|Rp|ر|WS\$|Rs|T\$|S\/\.|SR|Bs\.|NOK|CF|Fdj|£|¤|¥|SEK|Br|Bs|MTn|د\.أ\.|ден|den|RUB|أ\.م\.|лв\.|नेरू|DA|zł|Nfk|дин|дин\.|din|din\.|ر\.ي\.|US\$|Ksh|د\.ت\.|CFA|DT|MAD|B\/\.|NT\$|FCFA|soʻm|UM|Db|CVE|man\.|EC\$|PLN|රු\.|ر\.س\.|ج\.م\.|ر\.ع\.|¥|CA\$|ALL|Kč|د\.إ\.|դր\.|៛|د\.ك\.|ل\.ل\.|Afl\.|сом|LEI|kn|kr|kr\.|KM|Ft\.|VT|FC|ف\.ج\.ق\.|Fr\.|SFr\.|FCFP|m\.|ریال|FG|ج\.س\.|د\.ج\.|КМ|R\$|Lekë|৳|د\.ل\.|ل\.س\.|Nu\.|ман\.|₡|฿|₦|₩|ብር|₪|₫|€|₭|₮|₱|\$|S\$|₲|GEL|TRY|₴|₸|₹|₺|₽|Kz|LS|RF|MOP\$|GH₵|D|E|د\.ع\.|FBu|G|د\.م\.|Ft|K|RM|L|USh|P|Q|Le|R|S|Rs\.|NAf\.|DKK|؋|Ar|C\$|MK))/gm);
console.log(currency + value);
Upvotes: 0
Reputation: 281734
Use the regex
,\d*
It returns everything until the first comma.
$(function(){
$("em.price.product-card-price").text(function(index, text) {
alert(text.replace(/,\d*/, ''));
});
})
Upvotes: 0
Reputation: 12452
Why not use split
?
$("em.price.product-card-price").text(function(index, text) {
return text.split(',')[0];
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<em class="price product-card-price">1090,62€</em>
Maybe you want to add the €
again.
$("em.price.product-card-price").text(function(index, text) {
return text.split(',')[0] + text.substr(-1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<em class="price product-card-price">1090,62€</em>
Upvotes: 1