How to use Regular Expression on this form?

1

I have a question that arose from a solution. Use Tampermonkey to deselect files from a list; It works fine, but I wonder if I could apply regular expression to further expand its goal.

<div class="well">
  <h2>Select you files</h2>
  <table id="list-files" class="table table-bordered table-striped table-condensed table-hover">
    <tbody>
      <tr>
        <th width="10px"><input type="checkbox" id="select-all-files" checked="checked"></th>
        <th>File</th>
        <th>Size</th>
      </tr>
      <tr class="selectedFiles" style="display: table-row;">
        <td><input type="checkbox" name="files[]" value="0" ""></td>
        <td>
          <div><span class="pull-left">The.Blind.Side.2009.1080p.BluRay.x264.DTS-FGT/</span><span style="padding-top:1px;padding: 0 2px;display:block;overflow:hidden"><input style="width:100%;color:#c8c8c8;background-color:transparent;padding:0px;margin:0px" type="text" name="rename[0]" value="tuSITIO.com.txt"></span></div>
        </td>
        <td>34.0 iB</td>
      </tr>
      <tr class="selectedFiles" style="display: table-row;">
        <td><input type="checkbox" name="files[]" value="1" checked=""></td>
        <td>
          <div><span class="pull-left">The.Blind.Side.2009.1080p.BluRay.x264.DTS-FGT/</span><span style="padding-top:1px;padding: 0 2px;display:block;overflow:hidden"><input style="width:100%;color:#c8c8c8;background-color:transparent;padding:0px;margin:0px" type="text" name="rename[1]" value="The.Blind.Side.2009.1080p.BluRay.x264.DTS-FGT.mkv"></span></div>
        </td>
        <td>8.8 GiB</td>
      </tr>
      <tr class="selectedFiles" style="display: table-row;">
        <td><input type="checkbox" name="files[]" value="2" checked=""></td>
        <td>
          <div><span class="pull-left">The.Blind.Side.2009.1080p.BluRay.x264.DTS-FGT/</span><span style="padding-top:1px;padding: 0 2px;display:block;overflow:hidden"><input style="width:100%;color:#c8c8c8;background-color:transparent;padding:0px;margin:0px" type="text" name="rename[2]" value="The.Blind.Side.2009.1080p.BluRay.x264.DTS-FGT.nfo"></span></div>
        </td>
        <td>5.4 KiB</td>
      </tr>
    </tbody>
  </table>
</div>

Here we see an example where Script does its job correctly and, in this case, unchecks the "tuSITIO.com.txt" file.

My Script :

var inp=$("tr input");
var r="tuSITIO.com.txt";
for (i = 0; i < inp.length; i++) { 
 if(inp[i].value == r){
  inp[i-1].checked = false;
 }
}

This Script (by @Bryro) unchecks any file that is uploaded to a list works very well for exact values like the%%% example

Now ... How could you expand your goal using regular expressions? Suppose I do not want to include any file "tuSITIO.com.txt" , .txt , .nfo or .url . Is that possible?

For example, uncheck type files:

metadata.nfo
tu.sitio.nfo
qwerty(23).nfo
1a2b3c4d.url

I have seen some like: .website it occurs to me that perhaps putting: "([^\s]+(\.(?i)(jpg|png|gif|bmp))$)"; could result but I do not know how it could proceed, I appreciate your help and / or information, regards. Remember it's for Tampermonkey similar to Greasemonkey .

    
asked by Stack_qwerty 20.11.2018 в 04:18
source

1 answer

1

Since you explicitly requested regular expressions, based on this answer I got this:

/^.+\.([^.]|js|jpg|srt)$/

From it you can easily filter an array of file names with extensions (or exclude them) with the filter method . Here is an example:

const files = [
    "The.Blind.Side.2009.1080p.BluRay.x264.jpg",
    "texto.txt",
    "index.html",
    "The.Blind.Side.2009.1080p.BluRay.x264.srt",
];

files
  .filter(file => /^.+\.([^.]|js|jpg|html)$/.test(file))
  .forEach(file => console.log(file));

So, you can use it like this to solve your problem (with some changes to improve readability):

const inp = $("tr input");
//Solo en caso de que el resultado no sea un objeto Array
const files = Array.from(inp);
files
  .filter(({value: filename}) => {
      const extension = getFileExtension(filename);
      return isValidExtension(extension);
  })
  .forEach(item => item.checked = false);

function getFileExtension(filename) {
  const ext = /^.+\.([^.]+)$/.exec(filename);
  return ext == null ? "" : ext[1];
}

function isValidExtension(extension) {
  return /^js|jpg|html$/.test(extension)
}
    
answered by 20.11.2018 в 16:53