Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need help on implementing regex on a string.

Like if I have a string like this 'safsdfsd $sdfsdf$ fasdf $sdfdzs$' it should replace the $ with #.

I did something like this and it works

x.replace(/[$]/g,'#');


But I want it to be like if the $ is present at the beginning it should replace it with # and if the $ is at the end of a word it should replace with ##

So my string should go from 'fgsdfg $sdfsdgf$ sdfas $dgfdg$' to 'fgsdfg #sdfsdgf## sdfas #dgfdg##'

What I have tried:

Here's the code I tried

x.replace(/[$]/g,'#');
Posted
Updated 9-Nov-17 9:26am

You can;t do that in a single regex - you can detect "either/or" but you can't specify different replacements depending on which "catch" you get in a single replace operation.

So do it with two operations:
\$\b
will catch the "$" at the start of a word, and
\b\$
will catch the end.
 
Share this answer
 
Comments
SL-A-SH 9-Nov-17 4:09am    
Ok I did something like this x.replace(\$\b, '#'); but it gives me an error in the console 'Uncaught SyntaxError: Invalid or unexpected token'

Same with \b\$
OriginalGriff 9-Nov-17 4:18am    
:sigh:
Look at your javascript syntax...
SL-A-SH 9-Nov-17 4:18am    
Ok I got it now I didnt added the starting and end /. Thanks for the solution
OriginalGriff 9-Nov-17 4:34am    
You're welcome!
Not using Regex

var input = 'fgsdfg $sdfsdgf$ sdfas $dgfdg$';
       var words = input.split(' ');
       var tempOutput = [];
       for (var i = 0; i < words.length; i++) {
           var word = words[i];
           if (word[0] == '$')
               word = '#' + word.substring(1, word.length);
           if (word[word.length - 1] == '$')
               word = word.substring(0, word.length - 1) + '##';
           tempOutput.push(word);
       }
       var output = tempOutput.join(' ');
       alert(output)        //"fgsdfg #sdfsdgf## sdfas #dgfdg##"


Demo: - JSFiddle[^]
 
Share this answer
 
Comments
SL-A-SH 9-Nov-17 4:39am    
Thank you so much for this. The regex one is much simpler and less code.
Karthik_Mahalingam 9-Nov-17 4:39am    
cool, Ya agree :)
this is just an alternate solution. :)
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
 
Comments
Karthik_Mahalingam 9-Nov-17 20:00pm    
5Useful links
Patrice T 9-Nov-17 20:05pm    
thank you.
Indeed I collected them as help on RegEx questions.
I particularly like the last one :)
Karthik_Mahalingam 9-Nov-17 20:12pm    
perlre - perldoc.perl.org[^]
this one explains clearly, will go through completely when i get time.
have bookmarked this solution.
Patrice T 9-Nov-17 20:18pm    
Have a nice time.
Advice: go to last link paste a RegEx and enjoy the nice graph.
Karthik_Mahalingam 9-Nov-17 20:18pm    
sure :)

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