0

As you might know,Visual Studio's Find and Replace feature allows us to use Regular Expression but I don't know how to change something like this :

Math.round((document.getElementById('selectedproductPrixdock').value*document.getElementById('addxdock').value)*100)/100

to this one :

(Math.round((document.getElementById('selectedproductPrixdock').value*document.getElementById('addxdock').value)*100)/100).toFixed(2)

There are too much code like this on the page and changing them one by one is a big hassle.

Thanks in advance..

2
  • We need a bit more context -- is the first line an entire line, or in the middle of the line? How similar are the other target lines to this one? Commented Dec 4, 2009 at 19:07
  • Hi, the inner part may differ sometimes but the regex should not care about the inner part,all I need is to add ( at the beginning and ).toFixed(2) at the end of the given string. Commented Dec 4, 2009 at 20:20

3 Answers 3

1

This doesn't look like a very good candidate for regular expressions, as those are used to find/replace patterns. Deriving a pattern from this text would probably be a waste of time.

I'd just do this:

string s = "...some text...";
string toBeReplaced = "Math.round((document.getElementById('selectedproductPrixdock').value*document.getElementById('addxdock').value)*100)/100";
string replacement = "(" + toBeReplaced + ").toFixed(2)";
string result = s.Replace(toBeReplaced, replacement);

EDIT:

After re-reading your question, knowing each individual ID would make that tougher. Here's a Regex that should work:

string s = "...some text...";
string result = Regex.Replace(s, @"Math\.round\(\(document\.getElementById\('.+'\)\.value*document\.getElementById\('.+'\).value\)*100\)/100", "($0).toFixed(2)");
Sign up to request clarification or add additional context in comments.

Comments

0

How similar are the things you're trying to match?

If they're all identical except for the inner arguments, then

s/Math.round\(\(document.getElementById\('(.*?)'\).value*document.getElementById\('(.*?)'\).value\)*100\)\/100/\(Math.round\(\(document.getElementById\('$1'\).value*document.getElementById\('$2'\).value\)*100\)\/100\).toFixed\(2\)/g

Replacing $1 and $2 with whatever VS uses to fill in backreferences.

Comments

0

I think you're asking too much of regex in general.

In related news, you would likely have an easier time refactoring if you didn't pack fifty instructions into a single line.

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.