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
Add a ment  | 

4 Answers 4

Reset to default 12

Your 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