0

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?

3
  • I have been trying substring and string.replace combination but they don't work Commented Jun 2, 2013 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). Commented Jun 2, 2013 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? Commented Jun 2, 2013 at 9:50

3 Answers 3

4
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=[^:]*/,'');
Sign up to request clarification or add additional context in comments.

9 Comments

My understanding is that the second example in the question is a second possible input, not the expected output.
@nnnnn what does it mean by (^|:) and [^:]* ?
There's plenty of JS regex documentation available.
@DotnetSparrow: (^|:)FeeAmount = Match FeeAmount at the beginning of the string (^), or after a :. The [^:]* means "zero or more characters that are NOT :".
@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.)
|
2

How about this?

var input = 'frmSearch=FeeType=RecordingSpend:LoginID=:PersonCalled=:FeeAmount=22.234567:Paid=';
var output = input.replace(/:FeeAmount=[0-9.]*/, '');

Comments

0

According to the question, the matching regex rule may be

':FeeAmount=[^:]*'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.