简体   繁体   中英

Is there any way to erase the first part of this string?

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

If your string is always is of same nature you can use split

 let str = "$100.00 - $209.00" let op = str.split('$').pop() console.log(op)

You can use match

.*\$(\d+(?:\.\d+)?)

在此处输入图片说明

 let str = "$100.00 - $209.00" let op = str.match(/.*\\$(\\d+(?:\\.\\d+)?)/) console.log(op[1])

'$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM