Yet another 'negation matching'/'match everything except' issue in Java Script.
So here's what I want to do:
I have a huge text file and I want to remove everything from the file except the username/password lines. The following is a sample part from the text:
<property name="password">QWERTY</property>
....lots of similar tags......
<property name="username">Hello</property>
<property name="passive">1</property>
<property name="password">Test Password</property>
<property name="scheme">smb</property>
<property name="timeout">10000</property>
<property name="username">RANDOM USER</property>
....lots of similar tags......
<property name="username">Sid</property>
I want to remove each and every line which is not the password or the username.
I tried the following replace function to at least start off with the password but it didn't seem to work:
incomingString = incomingString.replace(/[\W\w]*?(?=<property name="password">[\W\w]*?</property).*?/g,"");
Looking back I can understand there are far too many issues with the regex so I wished to know a working regex that would help me remove all the lines in the previously mentioned text and leave me with
<property name="password">QWERTY</property>
<property name="username">Hello</property>
<property name="password">Test Password</property>
<property name="username">RANDOM USER</property>
<property name="username">Sid</property>
PS: It is important that their order in the document should be maintained
I went through a few questions on SO about this unending issue in JavaScript regex (this and lookbehinds)but the answers were very specific to that particular case.
Any help would be appreciated.
Thanks.