Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a string in javascript like this:

frmSearch=FeeType=RecordingSpend:LoginID=:PersonCalled=:FeeAmount=22.234567:Paid=

I want to remove :FeeAmount=22.234567 part from this string using Regex or string.replace.

It can be empty like this:

frmSearch=FeeType=RecordingSpend:LoginID=:PersonCalled=:FeeAmount=:Paid=

or it can contain any value

I tried this:

var str= frmSearch.substr(frmSearch.indexOf(":FeeAmount"), frmSearch.indexOf(":Paid="));

How can I do it using regex?

share|improve this question
3  
What did you try? –  Prinzhorn Jun 2 '13 at 8:13
    
I have been trying substring and string.replace combination but they don't work –  DotnetSparrow Jun 2 '13 at 8:13
1  
@DotnetSparrow: Show the effort, so people can help you figure out where you went wrong (and so this doesn't look like a "please write this for me" question). –  T.J. Crowder Jun 2 '13 at 8:14
    
@T.J.Crowder - It should've worked with .substring() or .slice(), both of which take a start and end index, but .substr() takes a start and length. DotnetSparrow - is your desired output the original string minus that part (as assumed by the answers jcsanyi and I posted), or is the FeeAmount=... the part you want to keep? –  nnnnnn Jun 2 '13 at 9:50
    
@nnnnnn: Doh! :-) –  T.J. Crowder Jun 2 '13 at 10:06
add comment

3 Answers

up vote 4 down vote accepted
var frmSearch = "FeeType=RecordingSpend:LoginID=:PersonCalled=:FeeAmount=22.234567:Paid=";
frmSearch = frmSearch.replace(/(^|:)FeeAmount=[^:]*/,'$1FeeAmount=');

...will leave FeeAmount= in the string. To remove it completely:

frmSearch = frmSearch.replace(/(^|:)FeeAmount=[^:]*/,'');
share|improve this answer
1  
My understanding is that the second example in the question is a second possible input, not the expected output. –  jcsanyi Jun 2 '13 at 8:16
    
@nnnnn what does it mean by (^|:) and [^:]* ? –  DotnetSparrow Jun 2 '13 at 8:20
1  
There's plenty of JS regex documentation available. –  nnnnnn Jun 2 '13 at 8:20
    
@DotnetSparrow: (^|:)FeeAmount = Match FeeAmount at the beginning of the string (^), or after a :. The [^:]* means "zero or more characters that are NOT :". –  T.J. Crowder Jun 2 '13 at 8:21
1  
@jcsanyi - Yes. It wasn't a problem when I thought the idea was to leave an "empty" FeeAmount= in the string, but I don't have time now to fix the second version. (I'm off to cook dinner.) –  nnnnnn Jun 2 '13 at 8:24
show 6 more comments

How about this?

var input = 'frmSearch=FeeType=RecordingSpend:LoginID=:PersonCalled=:FeeAmount=22.234567:Paid=';
var output = input.replace(/:FeeAmount=[0-9.]*/, '');
share|improve this answer
add comment

According to the question, the matching regex rule may be

':FeeAmount=[^:]*'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.