Мне было передано большое количество текстовых файлов, которые были созданы из электронных таблиц. Поэтому каждая строка данного текстового файла является одной строкой электронной таблицы.

Каждая строка содержит один или несколько экземпляров HTML-изображения в формате <img src='http://www.domainname.com/people2/images/somefilename.jpeg' alt=''width='100' style='border:none;' />

Мне нужно вычеркнуть все в каждой строке, кроме имен файлов и их расширений. Расширения файлов не все одинаковые типы или даже написание.

Пример списка:

Some text <img src='http://www.domainname.com/people2/images/somefilename1.jpeg' alt=''width='100' style='border:none;' /> more text.
Even more text <img src='http://www.domainname.com/people2/images/somefilename2.jpg' alt=''width='100' style='border:none;' /> and so on.
Third line now, and more text <img src='http://www.domainname.com/people2/images/somefilename3.png' alt=''width='100' style='border:none;' /> with extra image <img src='http://www.domainname.com/people2/images/somefilename4.png' alt=''width='100' style='border:none;' /> and text.

Необходимо превратить в:

somefilename1.jpeg
somefilename2.jpg
somefilename3.pngsomefilename4.png
-1
user1729506 28 Фев 2017 в 23:47

2 ответа

Лучший ответ

Решение с использованием RegExp Функция .prototype.exec () :

var str =  "Some text <img src='http://www.domainname.com/people2/images/somefilename1.jpeg' alt=''width='100' style='border:none;' /> more text.\
Even more text <img src='http://www.domainname.com/people2/images/somefilename2.jpg' alt=''width='100' style='border:none;' /> and so on.\
Third line now, and more text <img src='http://www.domainname.com/people2/images/somefilename3.png' alt=''width='100' style='border:none;' /> with extra image <img src='http://www.domainname.com/people2/images/somefilename4.png' alt=''width='100' style='border:none;' /> and text.";
    var re = /<img.*?\/images\/([^'"]+).*?/gm, m,
    result = "";

while ((m = re.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
    result += ' ' + m[1];
}

console.log(result);
2
RomanPerekhrest 28 Фев 2017 в 21:16

Вы можете использовать регулярное выражение, следуя регулярному выражению, предположите, что входные строки согласованы (с префиксом / images /):

var regex = /\/images\/([^'"]+)/g;
var results = [];
var match;
// "input" is a string from your text file.
while ((match = regex.exec(input)) !== null) {
  results.push(match[1]);
}
console.log(results);
0
engineforce 28 Фев 2017 в 21:26