Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i trim a sub string using java script?

i have a following value i want to trim "mi" and other white values from the below figure.

value= 8.8 mi

the value may change every time but will come with mi.

like image 789
NoviceToDotNet Avatar asked May 23 '26 04:05

NoviceToDotNet


2 Answers

What about:

var value = "8.8 mi";
var number = parseFloat(value); // 8.8

Looks like what you are looking for here.

like image 73
A. Wolff Avatar answered May 24 '26 17:05

A. Wolff


Use regex to remove the text from the end of the string:

var expr = / mi$/;
var value2 = value.replace(expr, "");

And like h2ooooooo commented, you can use a group to match on:

var expr = / (hr|mi|sec)$/;
like image 36
Patrick Hofman Avatar answered May 24 '26 17:05

Patrick Hofman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!