Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正则表达式与时间“12:30PM”的上午/下午情况匹配_Javascript_Regex - Fatal编程技术网

Javascript 正则表达式与时间“12:30PM”的上午/下午情况匹配

Javascript 正则表达式与时间“12:30PM”的上午/下午情况匹配,javascript,regex,Javascript,Regex,这里我有一个时间,格式是hh:mma。AM/PM不使用空格和时间分隔。现在,如何使用正则表达式使用javascript获取AM/PM和时间。您不需要正则表达式来获取值: var time = "12:30PM", suffix = time.substr(-2), numbers = time.substr(0, time.length-2); 结果: console.log(suffix); // "PM" console.log(numbers); // "12:30"

这里我有一个时间,格式是hh:mma。AM/PM不使用空格和时间分隔。现在,如何使用正则表达式使用javascript获取AM/PM和时间。

您不需要正则表达式来获取值:

var time = "12:30PM",
    suffix = time.substr(-2),
    numbers = time.substr(0, time.length-2);
结果:

console.log(suffix);  // "PM"
console.log(numbers); // "12:30"
如果希望小时/分钟在单独的变量中:

var temp = numbers.split(':'),
    hours = parseInt(temp[0], 10),   // 12
    minutes = parseInt(temp[1], 10); // 30

您有只包含该时间格式的字符串还是包含更多内容的字符串?的可能重复项,它不使用jQuery。字符串只包含时间格式12:30AM,时间和AM/PM之间没有空格。如何使用REGEXhow获得小时数、分钟数、am/pm如何分别获得小时数和分钟数你可以完全给出它,什么是10,完全原谅它?10是的基数参数,这确保第一个参数被解释为十进制数。否,.substr-2返回该字符串的最后2个字符。在这种情况下,PM;所以它不同于子串方法