Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

regex to remove 1 character from string conditional

given the string:
"the value is $14 and not $dollar"

I want to remove the $ from the $dollar (characters) not the $14 (numbers).
should look like this:
"the value is $14 and not dollar"

I have this, but it removes the first letter next to the $dollar:

replace(/\$[^0-9]/g, '');

0
TARJr
Asked:
TARJr
2 Solutions
 
Marco GasiFreelancerCommented:
Try this

$result = preg_replace('/\$(?=[a-zA-Z]+)/', '', $subject);
0
 
leakim971PluritechnicianCommented:
what don't you want from this one?
http://jsfiddle.net/ddsC9/2/
0
 
TARJrAuthor Commented:
margusG, that is almost perfect, but it only removes one occurrence of the $word.

if given: "the value is $14 and not $dollar but can be $this"

results: "the value is $14 and not dollar but can be $this"

0
 
sonawanekiranCommented:
try this
$result = preg_replace('/\$(?=[a-zA-Z]+)/g', '', $subject);
0
 
TARJrAuthor Commented:
bingo, I forgot the Global flag. /g
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now