20

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.

0

4 Answers 4

38

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Great thanks! I finally have understood the use parentheses and $ in substitutions...
Can you please show the solution to the question as an example? Otherwise this is pretty vague if you're not familiar with "Capture" and regex substitutions.
The main learning effect is finding "capture groups" in the manual of your choice...
That's awesome thank you! I wanted 123456,SomeText to become 123456,InsertedText,SomeText. What I did to solve this is below. Find: (123456),SomeText Replace: $1,InsertedText,SomeText
Could an actual answer rather than a "General answer" be given?
10

You can use lookaround assertions:

.replace(/(?<=\.)\w+(?=\()/g, 'xxx')

Those will allow the match to succeed while at the same time not being part of the match itself. Thus you're replacing only the part in between.

The easier option for people unfamiliar with regexes is probably to just include the . and ( in the replacement as well:

.replace(/\.\w+\(/g, ".xxx(")

5 Comments

Kind of a workaround for not using \1, isn't it? I mean, too many people do not know about lookahead and lookbehind, but often they are associated with a higher cost. In particular here, when they are at the beginning or the endo of the string to be replaced anyway.
Maybe. There are two pieces to keep, though, so you need essentially -replace '(\.)\w+(\()', '$1xxx$2' (sorry, falling back to PowerShell). I'm doubt that's more readable than -replace '\.\w+\(', '.xxx('. In any case, lookaround wouldn't be noticeably more expensive (if at all), and teaching people new concepts can hardly be considered wrong.
Yeah, I missed that the strings even are fixed strings. Then it is of course easier to just include them right away. Still, he should learn about the use of capture groups, e.g. to swap two values.
Given questions like these they should learn a lot of things, I guess ;-)
This approach using lookahead and lookbehind is very much appropriate. If learned properly these are useful in many situations. Here is simple and well explained tutorial on lookahead and lookbehind
2

I'd suggest a slightly different approach:

"myObject.myMethod();".replace(/^([^\.]*\.)\w+(\(.*)$/g, "$1xxx$2");

though simpler solutions have been suggested.

4 Comments

Well, that is the one I also suggested. Capture groups are a key feature of regexps, everyone should know.
@Anony-Mousse: Agreed, but I'm a bit confused about the \1 in your answer. IIRC, that's only the way to reference a group within the regex itself and not the replacement string, which requires $1. Please correct me if I'm wrong about that.
This depends on your dialect of regular expressions. I guess JS uses $1 only. Perl does both, and sed uses \1 IIRC.
Right, ok. I guess I was only talking with regard to JS.
1

That's awesome thank you Has QUIT--Anony-Mousse!

I wanted 123456,SomeText to become 123456,InsertedText,SomeText.

What I did to solve this is below. Credit to "Has QUIT--Anony-Mousse" above. I'm just giving a clear example of how to implement his solution.

Find:

(123456),SomeText

Replace:

$1,InsertedText,SomeText

And the result you will get from this find and replace using Regex is:

123456,InsertedText,SomeText

By using the parentheses around 123456, it basically assigned 123456 to a variable that I was able to use by using "$1" in the replace. Hope that makes sense. Thanks again!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.