The following code is returning this: $100.00 - $209.00 but I need just the last value(without the dollar sign: 209.00
I'm sure that that's possible with regular expression. Does someone know how to do it?
The variable comes from Handlebar.
<span class="round">
{{price<?php echo $priceKey; ?>_formatted}}
</span>
I'm expecting to transform the original to 209.00
I have to use javascript.
It's not necessarily regular expression. Could be anything that solves the issue.
Thanks
'$100.00 - $209.00'.split('$')[2]
isn't regex, but maybe the simplicity makes it better.
console.log('$100.00 - $209.00'.split('$')[2])
I use [2]
instead of [1]
because of this behavior of the .split
function :
If separator appears at the beginning or end of the string, or both, the array begins, ends, or both begins and ends, respectively, with an empty string.
Another alternative (without a regular expression) is to use String.slice() in conjunction with String.lastIndexOf() :
let str = document.querySelector(".round").innerText; let formatted = str.slice(str.lastIndexOf("$") + 1); console.log(formatted);
.as-console {background-color:black !important; color:lime;}
<span class="round"> $100.00 - $209.00 </span>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.