【发布时间】:2017-05-02 02:19:45
【问题描述】:
所以,我有一个字符串,当该行包含我正在搜索的单词时,我想返回一整行。 我有这个代码:
var subtitle = '4'+
'00:00:24.067 --> 00:00:35.924'+
'Hi, how are you?'+
'Doing fine?'+
''+
'5'+
'00:00:35.926 --> 00:00:47.264'+
'I\'m doing the best I can.'+
'And you?';
var myRe = /^.* you.*$/ig;
var myArray;
while ((myArray = myRe.exec(legenda)) !== null) {
var msg = 'Found ' + myArray[0] + '. ';
msg += 'Next match starts at ' + myRe.lastIndex;
console.log(msg);
}
在上面的代码中,我试图返回包含单词“you”的两行,预期的输出将是:“嗨,你好吗?” “你呢?”但我得到了 subtitle 变量的所有内容。 但是,如果我检查我的正则表达式 here,我会得到想要的输出。
有人可以帮帮我吗?这是我第一次使用正则表达式,感觉有点失落。
【问题讨论】:
-
这不是多行字符串。它是多行的,因为您的代码是多行,但字符串本身不是。字符串中没有一个换行符。
-
变量
subtitle是一个没有换行符的完整字符串。 -
你说得对,我会改正的!
标签: javascript regex