I have this code where I tried to remove the complete line if there is one argument and whose value is Window: String
const str = `fnct1(param: Int, Window: String): String func2(Window: String): String function3(param: String): String`;
const regex = /[^\s(]+\(Window:\s*String\): [^(\s]+\s*/gm;
const result = str.replace(regex, '');
console.log(result);
This worked for me and returns fnct1(param: Int, Window: String): String function3(param: String): String
I have hardcode Window: String
in regex there, now I want to make it generic because type can be anything and do same which I did for Window: String
.
It could be
const str = `fnct1(param: Int, Window: String): String func2(Window: Int): String function3(param: String): String`;
const str = `fnct1(param: Int, Window: String): String func2(Window: String): String function3(param: String): String`;
Does anyone know how to do that ?
You are looking to replace any 1 or more words after Window:
and 0+ whitespaces.
You may use
/[^\s(]+\(Window:\s*\w+\):\s*[^(\s]+\s*/g
See the regex demo.
var str = "fnct1(param: Int, Window: String): String func2(Window: String): String function3(param: String): String";
var regex = /[^\s(]+\(Window:\s*\w+\):\s*[^(\s]+\s*/g;
var result = str.replace(regex, '');
console.log(result);
Pattern details
[^\s(]+
- 1 or more chars other than whitespace and (
\(Window:
- a (Window:
substring\s*
- 0+ whitespaces\w+
- 1 or more word chars (ASCII letters, digits or/and _
)\):
- a ):
substring\s*
- 0+ whitespaces[^(\s]+
- 1 or more chars other than whitespace and (
\s*
- 0+ whitespacesCollected from the Internet
Please contact [email protected] to delete if infringement.
Comments