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
For notepad++:
search:
document\.all\[\'(.*)\'\]
replace with:
document.getElementById('\1')
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.
Search for:
document\.all\['([^']+)'\]
Replace with:
document.getElementById('\1')
Related
I want to remove everthing between .com/ to ?utm
How can I do that?
I use Notepad++ and Kate.
https://www.forexample.com/cat.belt?utm2900&eav=AfbHJya2K7Mbg2mPWatN
https://www.forexample.com/cat.food?utm89748&eav=AfbHJya2K7Mbg2mPWatN
https://www.forexample.com/dog.necklace?utm25875&eav=AfbHJya2K7Mbg2mPWatN
https://www.forexample.com/dog.belt?utm25285&eav=AfbHJya2K7Mbg2mPWatN
https://www.forexample.com/dog.food?utm785844&eav=AfbHJya2K7Mbg2mPWatN
I tried to Google the solution, but nothing really works.
In that case, you could use (?:^.*?\.com\/)(.*?)(?:\?utm.*$) with $1 matching "cat.belt", "cat.food" etc.
In fact, the only change in comparison to #akash 's answer is that you invert the capturing and non-capturing groups.
try the pattern: (^.*?\.com\/)(?:.*?)(\?utm.*$)
substitution: $1$2
demo here: https://regex101.com/r/xChAme/2
I'm trying to search-and-replace wit a regex in a file in Winmerge, however I can't find the right format for making a backreference.
For example I search (\w+), and I tried to replace by "$1", "\1", "\\1", "${1}", but neither of those works.
Any idea?
Thanks in advance,
format is \1 and you need winmerge 2.15.2
https://sourceforge.net/p/winmerge/bugs/2172/
Winmerge does not support replacing with regex. You can only use regex as find filters, which are facilitated with PCRE.
I have a large text file which contains many timestamps. The timestamps look like this: 2013/11/14 06:52:38AM. I need to remove the last two characters (am/pm/AM/PM) from each of these. The problem is that a simple find and replace of "AM" may remove text from other parts of the file (which contains a lot of other text).
I have done a find using the regular expression (:\d\d[ap]m), which in the above example would track down the last bit of the timestamp: :38AM. I now need to replace this with :38, but I don't know how this is done (allowing for any combination of two digits after the colon).
Any help would be much appreciated.
EDIT: What I needed was to replace (:\d\d)[ap]m with \1
Make (:\d\d[ap]m) into (:\d\d)[ap]m and use $1 not \1
Go to Search > Replace menu (shortcut CTRL+H) and do the following:
Find what:
[0-9]{2}\K[AP]M
Replace:
[leave empty]
Select radio button "Regular Expression"
Then press Replace All
You can test it at regex101.
Note: the use of [0-9] is generally better than \d (read why), and avoiding to use a capture group $1 with the use of \K is considered better. It's definitely not important in your case, but it is good to know :)
I have a text file with several lines like these ones:
cd_cod_bus
nm_number_ex
cd_goal
And I want to get rid of the - and uppercase the following character using Notepad++ (I can also use other tool but if it doesn't get the problem more troublesome).
So I tried to get the characters with the following regex (?<=_)\w and replace it using \U\1\E\2 for the uppercasing trick but here is where my problems came. I think the regex is OK but once I click replace all I get this result:
cd_od_us
nm_umber_x
cd_oal
as you can see it is only deleting the match.
Do you know where the problem is?
Thanks.
The search regex has no capture groups, i.e. the \1 and \2 references in the replacement do not refer to anything.
Try this instead:
Search: _(\w)
Replace \U\1\E
There you have a capture group in the search part (the parenthesis around the \w) and the \1 in the replacement refers back to what was captured.
replace
_(.)
with
\U$1
will give you:
cdCodBus
nmNumberEx
cdGoal
and for your
I can also use other tool but if it doesn't get the problem more troublesome
I suggest you try vim.
Try this,
_(\w)
and replace with
\U\1
here's a screenshot
I got a string like this:
PREFIX-('STRING WITH SPACES TO REPLACE')
and i need this:
PREFIX-('STRING_WITH_SPACES_TO_REPLACE')
I'm using Notepad++ for the Regex Search and Replace, but i'm shure every other Editor capable of regex replacements can do it to.
I'm using:
PREFIX-\('(.*)(\s)(.*)'\)
for search and
PREFIX-('\1_\3')
for replace
but that replaces only one space from the string.
The regex search feature in Notepad++ is very, very weak. The only way I can see to do this in NPP is to manually select the part of the text you want to work on, then do a standard find/replace with the In selection box checked.
Alternatively, you can run the document through an external script, or you can get a better editor. EditPad Pro has the best regex support I've ever seen in an editor. It's not free, but it's worth paying for. In EPP all I had to do was this:
search: ((?:PREFIX-\('|\G)[^\s']+)\s+
replace: $1_
EDIT: \G matches the position where the previous match ended, or the beginning of the input if there was no previous match. In other words, the first time you apply the regex, \G acts like \A. You can prevent that by adding a negative lookahead, like so:
((?:PREFIX-\('|(?!\A)\G)[^\s']+)\s+
If you want to prevent a match at the very beginning of the text no matter what it starts with, you can move the lookahead outside the group:
(?!\A)((?:PREFIX-\('|\G)[^\s']+)\s+
And, just in case you were wondering, a lookbehind will work just as well as a lookahead:
((?:PREFIX-\('|(?<!\A)\G)[^\s']+)\s+
You have to keep matching from the beggining of the string untill you can match no more.
find /(PREFIX-\('[^\s']*)\s([^']*'\))/
replace $1_$2
like: while (/(PREFIX-\('[^\s']*)\s([^']*'\))/$1_$2/) {}
How about using Replace all for about 20 times? Or until you're sure no string contains more spaces
Due to nature of regex, it's not possible to do this in one step by normal regular expression.
But if I be in your place, I do such replaces in several steps:
find such patterns and mark them with special character
(Like replacing STRING WITH SPACES TO REPLACE with #STRING WITH SPACES TO REPLACE#
Replace #([^#\s]*)\s to #\1_ server times.
Remove markers!
I studied a little the regex tool in Notepad++ because I didn't know their possibilities.
I conclude that they aren't powerful enough to do what you want.
Your are obliged to learn and use a programming language having a real regex capability. There are a number of them. Personnaly, I use Python. It would take 1 mn to do what you want with it
You'd have to run the replace several times for each space but this regex will work
/(?<=PREFIX-\(')([^\s]+)\s+/g
Replace with
\1_ or $1_
See it working at http://refiddle.com/10z