please consider the following javascript code:
"myObject.myMethod();".replace(/\.\w+\(/g, "xxx");
it gives "myObjectxxx);
" as ".myMethod(
" is selected.
Now I would only select myMethod
instead. In other words I want to select any word starting with .
and ending with (
(excluded).
Thanks, Luca.
To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring. The string 3foobar4 matches the regex /\d.
For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.
How do you ignore something in regex? To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.
General answer: Capture the part that you want to keep with parentheses, and include it in the substitution string as $1
.
See any regexp substitution tutorial for details.
Here: just include the .
and the (
in your substitution string.
For an exercise, write a regexp that will turn any string of the scheme --ABC--DEF--
to --DEF--ABC--
for arbitrary letter-values of ABC
and DEF
. So --XY--IJK--
should turn into --IJK--XY--
. Here you really need to use capture groups and back references.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With