tags:

views:

46

answers:

3

A simple (I hope) regex question:

I have several pages where I need to replace every document.all['string'] with document.getElementById('string').

I can use Visual Studio or Notepad++ to replace regular expressions, I just need the right ones.

Thanks for any assistance, Guy

A: 

Replace

document\.all\['(.*?)'\]

by

document.getElementById('$1')

The parentheses are used to identify a group. The $1 is used to print the value of the first group. The backslashes are used to escape special characters in regex. For the remnant it's pretty trivial.

Hope this helps.

BalusC
Don't forget to escape the `.` in `document.all`.
Asaph
I see. Updated it.
BalusC
Thanks for the help, but this doesn't work in a text editor or Visual Studio context (just tried both)
Traveling Tech Guy
Well, it apparently uses a different regex syntax. The above are perl-compatible.
BalusC
there is no nongreedy matches in notepad++ *? - doesn't work
x2
+1  A: 

Search for:

document\.all\['([^']+)'\]

Replace with:

document.getElementById('\1')
Asaph
Best answer cause of [^']
x2
@x2: so you aren't familiar with the *non-greedy* operator `.*?` as in my example?
BalusC
@BalusC: it's ok for perl, but not for built-in search in notepad++(don't know about vs)
x2
+1  A: 

For notepad++:

search:

document\.all\[\'(.*)\'\]

replace with:

document.getElementById('\1')
x2
Thanks a lot! Just saved me a heap of search-replace-test-aw-sh!t operations :)
Traveling Tech Guy
Be careful with this one. The `.*` is greedy and may extend the match beyond where you want it to. It will swallow everything up until the last instance of `']`.
Asaph