[英]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.注意:我使用
Intl.DateTimeFormat
来格式化日期和时间,但问题是我无法使用该 API 单独获取子午线值(对于给定日期)。 But I can get the meridian value (for the specified date) with the time string like below,但是我可以使用如下所示的时间字符串获取子午线值(对于指定日期),
-> 下午4:38 -> 下午4:38
-> 4.58 AM -> 4.58 上午
...so, I need to write two methods to get the meridian and time value from that strings. ...所以,我需要编写两种方法来从该字符串中获取子午线和时间值。
For example,例如,
getMeridian('下午4:38')
->下午
getMeridian('下午4:38')
->下午
getMeridian('4.58 AM')
-> AM
getMeridian('4.58 AM')
-> AM
getTimeWithoutMeridian('下午4:38')
-> 4:58
getTimeWithoutMeridian('下午4:38')
-> 4:58
getTimeWithoutMeridian('4.58 AM')
-> 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:如果您已经有了 getTimeWithoutMeridian,为什么不这样做:
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()
.下面的正则表达式帮助我获得
getTimeWithoutMeridian()
。
string.replace(/[^0-9:.]+/g, '')
Example,例子,
var string = '下午4:38'; var string = '下午4:38'; string.replace(/[^0-9:.]+/g, '');
string.replace(/[^0-9:.]+/g, ''); O/P: 4:58.
开/关:4:58。
I am still looking the answer for getMeridian('下午4:38')
->下午
我还在找
getMeridian('下午4:38')
的答案getMeridian('下午4:38')
->下午
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.