Solved

regex to remove 1 character from string conditional

Posted on 2011-09-30
5
173 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 31

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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 invoke it on a web page. 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.: (CODE)

828 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