Javascript正则表达式以双向方式分割字符串

发布于 2025-01-05 16:09:06 字数 997 浏览 0 评论 0原文

    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])/ 不能完全工作我需要知道如何让它在 hms 的“左”和“右”上分割

基本上,这个问题的答案是例如,将 "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

玩世 2025-01-12 16:09:06

你可以像...

var parts = "5h55m55s"
   .split(/(\d+)/).filter(function(a) { return a; });
   // ["5", "h", "55", "m", "55", "s"]

jsFiddle

这将下降 0 次。如果可能,请将 filter() 主体更改为 return a.length

You could something like...

var parts = "5h55m55s"
   .split(/(\d+)/).filter(function(a) { return a; });
   // ["5", "h", "55", "m", "55", "s"]

jsFiddle.

This will drop times of 0. If that is possible, change the filter() body to return a.length.

北音执念 2025-01-12 16:09:06
var pieces = "55m55s".match(/\d+|[hms]/g);
// ["55", "m", "55", "s"]

var pieces = "5h55m55s".match(/\d+|[hms]/g);
// ["5", "h", "55", "m", "55", "s"]
var pieces = "55m55s".match(/\d+|[hms]/g);
// ["55", "m", "55", "s"]

var pieces = "5h55m55s".match(/\d+|[hms]/g);
// ["5", "h", "55", "m", "55", "s"]
澜川若宁 2025-01-12 16:09:06
time = "5h55m55s";
pattern = /(\d*h)(\d*m)(\d*s)/i
timeArry=time.match(pattern);
console.log(timeArry.splice(1,3));​ //Output ["5h", "55m", "55s"]

http://jsfiddle.net/eteuz/

time = "5h55m55s";
pattern = /(\d*h)(\d*m)(\d*s)/i
timeArry=time.match(pattern);
console.log(timeArry.splice(1,3));​ //Output ["5h", "55m", "55s"]

http://jsfiddle.net/eteuz/

故事与诗 2025-01-12 16:09:06

要使用正则表达式执行此操作:

var regex = /(\d+)h(\d+)m(\d+)s/g
var time = _GET('t', '0').match(regex)
var hours = time[0]
var minutes = time[1]
var seconds = time[2]
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)​

(\d+)h 查找文字h 字符之前的数字。

(\d+)m 查找文字 m 字符之前的数字。

(\d+)s 查找文字 s 字符之前的数字。

To do it with a regular expression:

var regex = /(\d+)h(\d+)m(\d+)s/g
var time = _GET('t', '0').match(regex)
var hours = time[0]
var minutes = time[1]
var seconds = time[2]
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)​

(\d+)h finds digits that precede a literal h character.

(\d+)m finds digits that precede a literal m character.

(\d+)s finds digits that precede a literal s character.

表情可笑 2025-01-12 16:09:06

哇,我已经走了很长的路。

我前段时间必须实施这样的事情,答案实际上非常简单。尽管如此,它还是让我想起了这个问题。

因此,为了回答我自己的问题,这就是我的想法:

function parseTime(t){
  var h = RegExp('([0-9]*)h').exec(t),
      m = RegExp('([0-9]*)m').exec(t),
      s = RegExp('([0-9]*)s').exec(t),
      a = [];
  if(h){
    a.push('Hours: '+h[1]);
  }
  if(m){
    a.push('Minutes: '+m[1]);
  }
  if(s){
    a.push('Seconds: '+s[1]);
  }
  return a;
}

parseTime('1h23m45s'); // ['Hours: 1', 'Minutes: 23', 'Seconds: 45']
parseTime('1h45s');    // ['Hours: 1', 'Seconds: 45']
parseTime('2m')        // ['Minutes: 2']

它简单且动态。 很简单:

var _time = parseTime('12m14s');
console.log(_time.join('\n'));

另外,读出它

Minutes: 12
Seconds: 14

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:

function parseTime(t){
  var h = RegExp('([0-9]*)h').exec(t),
      m = RegExp('([0-9]*)m').exec(t),
      s = RegExp('([0-9]*)s').exec(t),
      a = [];
  if(h){
    a.push('Hours: '+h[1]);
  }
  if(m){
    a.push('Minutes: '+m[1]);
  }
  if(s){
    a.push('Seconds: '+s[1]);
  }
  return a;
}

parseTime('1h23m45s'); // ['Hours: 1', 'Minutes: 23', 'Seconds: 45']
parseTime('1h45s');    // ['Hours: 1', 'Seconds: 45']
parseTime('2m')        // ['Minutes: 2']

It's simple and dynamic. Plus, to read it out is as simple as:

var _time = parseTime('12m14s');
console.log(_time.join('\n'));

Which prints

Minutes: 12
Seconds: 14
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文