简体   繁体   中英

How to split a specific part from String(Javascript)?

Given the three formatted distance strings, how can I extract just the distance number?

x = "Max, 156km from you(001)"
a = "Nick, 66km from you(131)"
b = "Niclas, 76km from you(224)"
console.log(distance_from_x, distance_from_a, distance_from_b)
// Expected: (156,66,76) 

This should work

const regex = /[0-9]+km/gm;

You can use this regular expression:

(\\d+)km

example you can see here

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