admin管理员组文章数量:1244316
There is regex feature to find words instead of "Ctrl + F" in some editor like VS Code, I'm trying to find a word after a specific word with some another lines.
For example, how to use regex to filter those "someFunction" with the specific "message" property as below:
...
someFunction({
a: true,
b: false
})
someFunction({
a: true,
b: false,
c: false,
d: true,
message: 'I wnat to find the funciton with this property'
})
someFunction({
a: true
})
...
The regex I tried is like:
/someFunction[.*\s*]*message/
But it did't work
How can I achieve this aim?
There is regex feature to find words instead of "Ctrl + F" in some editor like VS Code, I'm trying to find a word after a specific word with some another lines.
For example, how to use regex to filter those "someFunction" with the specific "message" property as below:
...
someFunction({
a: true,
b: false
})
someFunction({
a: true,
b: false,
c: false,
d: true,
message: 'I wnat to find the funciton with this property'
})
someFunction({
a: true
})
...
The regex I tried is like:
/someFunction[.*\s*]*message/
But it did't work
How can I achieve this aim?
Share Improve this question edited May 8, 2019 at 4:11 Emma 27.7k11 gold badges48 silver badges71 bronze badges asked May 8, 2019 at 3:39 Chris KaoChris Kao 5036 silver badges15 bronze badges 2- What version of VS Code are you using? Multiline search was added in v1.29 (released November 2018), as per this answer. – Hoppeduppeanut Commented May 8, 2019 at 3:44
- Your reference link is correct! I need to use "\n" to enable the multiline search. The final regex I used is /someFunction[\S\n\s*]*message/ – Chris Kao Commented May 8, 2019 at 6:57
4 Answers
Reset to default 12Your expression is just fine, you might want to slightly modify it as:
someFunction[\S\s*]*message
If you wish to also get the property, this expression might work:
(someFunction[\S\s*]*message)(.*)
You can add additional boundaries, if you like, maybe using regex101..
Graph
This graph shows how your expression would work and you can visualize other expressions in jex.im:
Performance Test
This script returns the runtime of a string against the expression.
repeat = 1000000;
start = Date.now();
for (var i = repeat; i >= 0; i--) {
var string = "some other text someFunction \n \n message: 'I wnat to find the funciton with this property'";
var regex = /(.*)(someFunction[\S\s*]*message)(.*)/g;
var match = string.replace(regex, "$3");
}
end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match
本文标签:
javascriptRegEx for matching a word after specific word in multiple linesStack Overflow
版权声明:本文标题:javascript - RegEx for matching a word after specific word in multiple lines - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1740151929a2232820.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论