Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I have a string a=",1,2,3,123" & i want to replace ",12" and i want result a=",1,2,3,123" by using javascript how should i get
Posted
Updated 14-Aug-15 5:27am
v2
Comments
E.F. Nijboer 14-Aug-15 4:17am    
Looks like the value of a and the result you want are the same. Also, what have you tried so far?
Member 11205380 14-Aug-15 4:31am    
string A=",1,2,3,123"
i want to replace ",1" to "+" means i want result string="+,2,3,123"
but it gives result string="+,2,3,+23"
Richard MacCutchan 14-Aug-15 4:45am    
Yes, because you did a global change. You need to change only the first occurrence. See the JavaScript String Reference fro information.

replace ,1, with +, will give you the expected result
 
Share this answer
 
v2
You have to replace ",1," insted of ",1" to "+," like this:

var str = ",1,2,3,123";
var txt = str.replace(",1,","+,");
 
Share this answer
 

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