N Sharma
N Sharma

Reputation: 34497

How to remove complete line from the string on basis of string match using regex

I have this string

const str = "fnct1(param: Int, Window: String): String func2(Window: String): String function3(param: String): String"

I have removed the Window: String from fnct1(param: Int, Window: String): String by using the regular expression.

str.replace(/(, )?Window: String/g, '')

Now I want to remove the complete line if there is one argument and whose value is Window: String

It should give

fnct1(param: Int, Window: String): String
function3(param: String): String

Upvotes: 1

Views: 80

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Use the following approach (in case if all the lines are separated by a newline, i.e. each line is on separate line):

const str = `fnct1(param: Int, Window: String): String
func2(Window: String): String
function3(param: String): String`;
const regex = /^[^\s(]+\(Window:\s*String\): .+\n*/gm;
const result = str.replace(regex, '');

console.log(result);


For single-line text use the following approach:

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);

Upvotes: 2

Related Questions