Click here to Skip to main content
13,251,760 members (25,190 online)
Rate this:
 
Please Sign up or sign in to vote.
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 8-Nov-17 22:11pm
SL-A-SH207
Updated 9-Nov-17 10:26am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
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 Bangalore 9-Nov-17 4:39am
   
cool, Ya agree :)
this is just an alternate solution. :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.[^]
  Permalink  
Comments
Karthik Bangalore 9-Nov-17 20:00pm
   
5
Useful links
ppolymorphe 9-Nov-17 20:05pm
   
thank you.
Indeed I collected them as help on RegEx questions.
I particularly like the last one :)
Karthik Bangalore 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.
ppolymorphe 9-Nov-17 20:18pm
   
Have a nice time.
Advice: go to last link paste a RegEx and enjoy the nice graph.
Karthik Bangalore 9-Nov-17 20:18pm
   
sure :)
SL-A-SH 10-Nov-17 0:59am
   
Thank you so much for this. Will go through this sometime. :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 9 Nov 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100