Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have tried this
value.replace(/[^\$\d*]/g, '');

Its replacing all letter, special character etc. But If extra $ is given as input it is not replacing it.

e.g. If the input value is $686$7 It is not replacing the $ between 6 and 7.

I need the result should like be $6867

If the input is $$ the result should be $

If the input is $676$ o/p should be $676

If the input is $676$ o/p should be $676
If the input is $676h$ o/p should be $676

What I have tried:

value.replace(/[^\$\d*]/g, '');
Posted
Updated 2-Feb-17 20:37pm

1 solution

One way is: First, remove the starting $, replace all non digit from the remaining string, then pre-pend the $ to the digit-only string. e.g.
<script>
str = "$68ab6$7$"; 
withoutFirst$ = str.replace("$", "");
removeNonDigits = withoutFirst$.replace(/[^\d]/g, "");
alert("$" + removeNonDigits);
</script>
Demo at JSFiddle[^]
BTW, to find any word than starts with a $, try this regex pattern:
/^\$.+/g
 
Share this answer
 
v4
Comments
Karthik_Mahalingam 3-Feb-17 4:13am    
5
Peter Leow 3-Feb-17 4:44am    
Thank you, Karthik.

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