简体   繁体   English

如何使用javascript从字符串中拆分字符(所有语言环境)和数字?

[英]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.

相关问题 使用正则表达式拆分 javascript 字符串以将数字与其他字符分开 - split javascript string using a regexp to separate numbers from other characters 如何在Javascript中将一个字符串拆分为两个浮点数? - How can I split a string into two float numbers in Javascript? 如何使用 JavaScript 将大量数字拆分为 3 组数字? - How can I split a large number to 3 groups of numbers using JavaScript? 如何使用Javascript防止在输入上输入除数字以外的其他字符 - How can I prevent other characters from being inputted on an input except numbers using Javascript 如何将数字字符串与范围分开? - How can I split this string of numbers with ranges? 如何在javascript中将字符串中的所有'\\'和'/'字符替换为' - ' - How can i replace all '\' and '/' characters, in a string to '-', in javascript 如何使用javascript删除字符串中指定单词之后和句点之前的所有字符? - How can I remove all characters after a specified word and before a period in a string using javascript? 如何将字符串中的所有数字从 1-9 替换为 Javascript 中的拼写等效项? - How can I replace all numbers in a string from 1-9 to their spelled out equivalents in Javascript? Javascript删除字符串中不是数字,字母和空格的所有字符 - Javascript remove all characters from string which are not numbers, letters and whitespace 如何使用JavaScript将字符串分成4个变量 - How can I split a string into 4 variables using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM