Javascript regex match going in infinite loop - Stack Overflow

this is the code I am trying :var arr = [];var str = "hey check this video out! youtubewatch?v=1

this is the code I am trying :

var arr = [];
var str = "hey check this video out! youtube/watch?v=123456 and there is some other text youtube/watch?v=3t_3456 and some more.";
while (match = /youtube\\/watch\?v=([^\s]+)/.exec(str)) {
  arr.push(match[1]);
}
console.log(arr);

it should capture the last part of the url's and push them to an array.

the array I am expecting is :

["123456", "3t_3456"]

but this code is going in an infinite loop, what's wrong with it ?

this is the code I am trying :

var arr = [];
var str = "hey check this video out! youtube./watch?v=123456 and there is some other text youtube./watch?v=3t_3456 and some more.";
while (match = /youtube\.\/watch\?v=([^\s]+)/.exec(str)) {
  arr.push(match[1]);
}
console.log(arr);

it should capture the last part of the url's and push them to an array.

the array I am expecting is :

["123456", "3t_3456"]

but this code is going in an infinite loop, what's wrong with it ?

Share Improve this question asked May 21, 2014 at 6:10 aeloraelor 11.1k3 gold badges34 silver badges48 bronze badges 3
  • 2 You have a while loop performing the same action over and over with nothing changing its condition. You need to modify str or it will just keep checking the same string over and over. – JLRishe Commented May 21, 2014 at 6:15
  • but I took the code from stackoverflow./questions/432493/… – aelor Commented May 21, 2014 at 6:17
  • @JLRishe what should be the possible solution to my problem – aelor Commented May 21, 2014 at 6:17
Add a ment  | 

2 Answers 2

Reset to default 7

The difference between your code and the page you linked to is:

  • You are creating a new regex on every iteration of the loop, so it is not maintaining a record of the previous matches.
  • You are not using the g (global) flag, so even if you were not creating new regexes, it would just keep finding the first match.

You need to reuse the same regex, and use the g flag:

var pattern = /youtube\.\/watch\?v=([^\s]+)/g;
while (match = pattern.exec(str)) {
    arr.push(match[1]);
}

You are inviting an infinite loop without using the global flag in your regex.

var arr = [];
var str = "hey check this video out! youtube./watch?v=123456 and there is some other text youtube./watch?v=3t_3456 and some more.";
var re = /youtube\.\/watch\?v=([^\s]+)/g;
while (match = re.exec(str)) {
  arr.push(match[1]);
}
console.log(arr);

See a working example here.

Without the g flag you'd run into an infinite loop, see here (WARNING: Clicking this link may crash your browser.).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744219562a4563714.html

相关推荐

  • Javascript regex match going in infinite loop - Stack Overflow

    this is the code I am trying :var arr = [];var str = "hey check this video out! youtubewatch?v=1

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信