Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Let's say I have the following string;

$('test')...;
$('test notatest')...;
$('test notatest notatest2')...;
alert('this test should not be replaced');


What's the best way using Regex to replace 'test' with something else, only where it appears within $(...)?

What I have tried:

---------------------------------------------------------------
Posted
Updated 14-Jan-18 1:19am
Comments
Graeme_Grant 14-Jan-18 6:33am    
Have you tried reading tutorials, documentation, or googling for the answer?

Please take the time to look at these links provided before posting questions:
* Basic Debugging with Visual Studio 2010 - YouTube[^]
* Some guidelines for posting questions in the forums[^]
* Tales from the Evil Empire - Asking questions is a skill[^]
[no name] 14-Jan-18 7:14am    
Hmm. I don't remember asking for the regex, just for the best way using regex.

I wouldn't be here if I hadn't tried for several hours already.
Graeme_Grant 14-Jan-18 7:21am    
Always a good idea to share what you have tried...

Try this:
(?<=\$\(')(test)(?=.*?'\))

Use Regex.Replace, and it will replace the "test" with your string.

If you are going to work with Regexes, then get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
v2
Comments
[no name] 14-Jan-18 7:18am    
Excellent, thank you!

I'll have a look at Expresso, I've been meaning to find something which is capable of generating regex; I've only been using regex testers.
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900