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

N Sharma

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 ?

Wiktor Stribiżew

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+ whitespaces

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I remove a specific character from multi line string using regex in python

how can I remove generic type from class using refactoring

How to remove certain string from the original string using regex in Java

How to remove specific string from string using regex

How to remove a string from a line

remove line comment from file by using regex

how to remove file/folder path from string using regex

How to remove unmatched part from string using JS/jQUery regex

How to remove url starting with www.*** from a string using regex?

How to remove punctuation from a string with exceptions using regex in bash

How to remove html tags from an Html string using RegEx?

How do you remove .00 from a string using Regex function?

How to get groupdict from multi-line string using regex

How to remove from same type of string coming together using python

Remove tag from string variable using Regex

Remove data from String using Regex

Using regEx to remove digits from string

remove empty quotes from string using regex

Remove numbers from beginning of string using regex

Remove leading zeros from a string using regex

Remove digits from the string if they are concatenated using Regex

Removing Extra Line From String Using regex

How to remove an element from a list type Resource Registry record using Generic Setup

generic type from string

How to remove the backslash in string using regex in Java?

How to remove duplicate characters in a string using regex?

Using regex how to remove a date,time in a string?

How to search and remove a string using regex?

How to remove string "[xyz]" from filename with Regex?