Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to Replace Multiple Parts of a String (In Visual Studio 2012)

Basically I am updating an old web app to be multi browser compliant. All over the place the original programmers have used -

document.all.element

As I understand the ".all" is IE only, introduced in IE 4. The standards compliant ".getElementById" function was introduced in IE 5. I want to change instances of the above statement to -

document.getElementById("element")

How would I form this regular expression, specifically using Visual Studio's "Find and Replace" dialog?

like image 862
John C Avatar asked Sep 12 '12 10:09

John C


2 Answers

In Visual Studio 2010 and earlier the regular expression syntax is following:

To tag an expression use { }, to reference first tag for replacement use \1

In your case you need to find:

document\.all\.{[a-zA-Z0-9]+}

and replace with:

document.getElementById('\1')

In Visual Studio 2012 the regular expression syntax has changed to:

document\.all\.([a-zA-Z0-9]+)
document.getElementById('$1')
like image 69
Michal Klouda Avatar answered Sep 19 '22 17:09

Michal Klouda


I'm not sure if this is an issue with me using Visual Studio 2012 RC but using braces failed to match to anything.

I ended up using the find expression -

document\.all\.([a-zA-Z0-9]+)

and the replacement

document.getElementById("$1")

The "$1" refers to the bracketed expression.

Thanks for your help guys.

like image 43
John C Avatar answered Sep 21 '22 17:09

John C