Home > Mobile >  RegEx how to match everything exclude a string in a function
RegEx how to match everything exclude a string in a function

Time:12-05

How can I exclude a string from the match in regex?

Input:

functionXY("Hello World"); 

Match:

functionXY();

i got this far with my regex expression:

/functionXY[(][)][;]/gm

but can't find the correct expression to exclude the string "Hello World" in the match.

CodePudding user response:

You can use a regex to match everything inside the brackets, then use replace.

const str = 'functionXY("Hello World");';

const regex = /(?<=functionXY[(]). (?=[)][;])/gm;

const res = str.replace(regex, '')
console.log(res)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Your pattern functionXY[(][)][;] uses square brackets for single characters and only matches functionXY();

Instead you can use 2 capture groups to capture what you want to keep, and match the double quotes and all chars other than double quotes that you don't want to keep.

In the replacement use 2 capture groups $1$2

(functionXY\()"[^"]*"(\);)

Regex demo

  • Related