Kyle.Belanger Kyle.Belanger - 1 year ago 94
Javascript Question

Find / Replace Text Using Regexp

I have chunks of text that I need to remove, or simply replace with "".

Below is an excerpt of the text

  1. $prep->bindValue(':PYOtherExpensesAmt',$summaryData['*PYOtherExpensesAmt*'])?$row['PYOtherExpensesAmt']:''));

  2. $prep->bindValue(':CYOtherExpensesAmt',$summaryData['*CYOtherExpensesAmt*'])?$row['CYOtherExpensesAmt']:''));

I need to replace chunks of text starting with
and ending with

So example the first replacement would be:

  • $prep->bindValue(':PYOtherExpensesAmt',$summaryData['PYOtherExpensesAmt']);

I've tried the following regexp:

([?][$row], [))])

Any tips on how to get this remove these blocks of text as a regular expression?

Answer Source

Here is a regexp that will match your pattern:


Explanation: The backslash (\) will escape the character in front of it so that the engine will search for literally that character. This is needed because ?, $, and ) already have special meanings in the regexp syntax. Finally, the .*? part means that it will match zero or more (*) instances of any character (.) in a "lazy" manner (?), thus stopping at the first )). An excellent resource for learning regex syntax:

Then, as you guessed, you should replace all matches with an empty string to remove them.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download