简体   繁体   中英

How can I split the characters(all locales) and numbers from a string using javascript?

Note: I am using Intl.DateTimeFormat to format the date and time, but the problem there is I could't get the meridian value alone (for the given date) by using that API. But I can get the meridian value (for the specified date) with the time string like below,

-> 下午4:38

-> 4.58 AM

...so, I need to write two methods to get the meridian and time value from that strings.

For example,

getMeridian('下午4:38') ->下午

getMeridian('4.58 AM') -> AM

getTimeWithoutMeridian('下午4:38') -> 4:58

getTimeWithoutMeridian('4.58 AM') -> 4.58

Can anyone help me on this issue? Thanks!

If you already have getTimeWithoutMeridian, why not just do it like this:

var getMeridian = function(myString) { myString.replace(getTimeWithoutMeridian(myString), "") }

Which is just gonna remove the time string and leave the meridian

Below regex helps me to get the getTimeWithoutMeridian() .

string.replace(/[^0-9:.]+/g, '')

Example,

var string = '下午4:38'; string.replace(/[^0-9:.]+/g, ''); O/P: 4:58.

I am still looking the answer for getMeridian('下午4:38') ->下午

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