Solved

regex to remove 1 character from string conditional

Posted on 2011-09-30
5
169 Views
Last Modified: 2012-08-13
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
Comment
Question by:TARJr
5 Comments
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 125 total points
ID: 36892017
Try this

$result = preg_replace('/\$(?=[a-zA-Z]+)/', '', $subject);
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36892027
what don't you want from this one?
http://jsfiddle.net/ddsC9/2/
0
 
LVL 1

Author Comment

by:TARJr
ID: 36892353
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
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 125 total points
ID: 36892384
try this
$result = preg_replace('/\$(?=[a-zA-Z]+)/g', '', $subject);
0
 
LVL 1

Author Closing Comment

by:TARJr
ID: 36892405
bingo, I forgot the Global flag. /g
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now