Javascript正则表达式以双向方式分割字符串
var REGEX = new RegExp("(?=[hms])g") var TIME = _GET('t', '0').split(REGEX) var hours = TIME[TIME.indexOf('h')-1] var minutes = TIME[TIME.indexOf('m')-1] var seconds = TIME[TIME.indexOf('s')-1] var HOURS = hours?('hours: '+hours):'' var MINUTES = minutes?('minutes: '+minutes):'' var SECONDS = seconds?('seconds: '+seconds):'' document.write('TIME RECIEVED:<br><br>'+HOURS+'<br>'+MINUTES+'<br>'+SECONDS)
这是整个代码。我基本上想看看我是否可以模仿 youtube 的视频技巧(把 &t=XhYmZs 转到视频中的那个时间)
唯一缺少的是我不知道正则表达式 >_<
我需要的是将字符串 "5h55m55s"
拆分为 ['5','h','55','m','55','s']
而不是 ['5', 'h55', 'm55', 's']
,这破坏了我的代码。哦,是的,_GET 函数并不重要,它只是从 url 中获取一个字符串,就像 php 的 $_GET 变量一样,
所以显然,正则表达式 /(?=[hms])/
不能完全工作我需要知道如何让它在 h
、m
和 s
的“左”和“右”上分割
基本上,这个问题的答案是例如,将 "55m55s"
拆分为数组 ['55','m','55','s']
的正则表达式
var REGEX = new RegExp("(?=[hms])g") var TIME = _GET('t', '0').split(REGEX) var hours = TIME[TIME.indexOf('h')-1] var minutes = TIME[TIME.indexOf('m')-1] var seconds = TIME[TIME.indexOf('s')-1] var HOURS = hours?('hours: '+hours):'' var MINUTES = minutes?('minutes: '+minutes):'' var SECONDS = seconds?('seconds: '+seconds):'' document.write('TIME RECIEVED:<br><br>'+HOURS+'<br>'+MINUTES+'<br>'+SECONDS)
this is the entire code. I basically wanted to see if I could mimick youtube's video trick (put &t=XhYmZs to go to that time in a video)
The only missing thing is that I don't know regular expressions >_<
What I need is to split the string "5h55m55s"
into ['5','h','55','m','55','s']
instead of ['5', 'h55', 'm55', 's']
, which is breaks my code. Oh yeah, and the _GET function is unimportant, it just obtains a string from the url, like php's $_GET variable
So Obviously, the regex /(?=[hms])/
doesn't work completely and I need to know how to get it to split both on the "left" and "right" of h
, m
, and s
Basically, the answer to this question is a regexp that splits, for example, "55m55s"
into the array ['55','m','55','s']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以像...
jsFiddle。
这将下降
0
次。如果可能,请将filter()
主体更改为return a.length
。You could something like...
jsFiddle.
This will drop times of
0
. If that is possible, change thefilter()
body toreturn a.length
.http://jsfiddle.net/eteuz/
http://jsfiddle.net/eteuz/
要使用正则表达式执行此操作:
(\d+)h
查找文字h
字符之前的数字。(\d+)m
查找文字m
字符之前的数字。(\d+)s
查找文字s
字符之前的数字。To do it with a regular expression:
(\d+)h
finds digits that precede a literalh
character.(\d+)m
finds digits that precede a literalm
character.(\d+)s
finds digits that precede a literals
character.哇,我已经走了很长的路。
我前段时间必须实施这样的事情,答案实际上非常简单。尽管如此,它还是让我想起了这个问题。
因此,为了回答我自己的问题,这就是我的想法:
它简单且动态。 很简单:
另外,读出它
Wow, I've come a long way.
I had to implement something like this some time ago, and the answer is actually really simple. Nonetheless, it reminded me of this question.
So, to answer my own question, here's what I cooked up:
It's simple and dynamic. Plus, to read it out is as simple as:
Which prints