Kyle.Belanger Kyle.Belanger - 3 months ago 15
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
?$row
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

Here is a regexp that will match your pattern:

\?\$row.*?\)\)

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: regular-expressions.info

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

Comments