regex 替换和清理.srt文件

lstz6jyr  于 2022-11-26  发布在  其他
关注(0)|答案(3)|浏览(153)

我有一个.srt文件,其文本如下:
19
00:01:05,100--〉00:01:08,820(中文)
西班牙等国家。另一个
20
00:01:08,820--〉00:01:11,850(中文)
是西班牙海岸的南端
21
00:01:11,850--〉00:01:15,060(中文)
如此接近北方
我发现这个代码在清理信息方面工作得很好,但是这个代码保留了初始数字(这些数字可以从一位到四位)
结果是:
另一个要考虑的因素是西班牙海岸的南端离北方如此之近,可能会
知道怎么去掉手指吗?
这是我的代码:

<script>
            document.querySelector('#files').addEventListener('change', (e) => {
                
                let files = e.target.files,
                    i = 0,
                    reader = new FileReader;
            
                
                reader.onload = (e) => {
                    //console.log(files[i].name, e.target.result);
                    var fileName = files[i].name;
                    var text = e.target.result;

                    text = text.replace(/WEBVTT[\r\n]/,"");
                    text = text.replace(/NOTE duration:.*[\r\n]/,"");
                    text = text.replace(/NOTE language:.*[\r\n]/,"");
                    text = text.replace(/NOTE Confidence:.+\d/g,"");
                    text = text.replace(/NOTE recognizability.+\d/g,"");
                    text = text.replace(/[\r\n].+-.+-.+-.+-.+/g,"");
                    text = text.replace(/[\r\n].+ --> .+[\r\n]/g,"");
                    text = text.replace(/.[\r\n]. --> .+[\r\n]/g,"");
                    text = text.replace(/[\n](.)/g," $1");
                    text = text.replace(/[\r\n]+/g,"");
                    text = text.replace(/^ /,"");
                
                    var heading = document.createElement('h3');
                    document.body.appendChild(heading);
                    heading.innerHTML = "Transcript for '" + files[i].name + "'";
                
                    var copyButton = document.createElement('button');
                    document.body.appendChild(copyButton);
                    copyButton.onclick = function() {copyToClip(text,fileName); };
                    copyButton.innerHTML = "Copy transcript";
                    copyButton.className = "copyButton";
                
                    var div = document.createElement('div');
                    document.body.appendChild(div);
                    div.className = "cleanVTTText";
                    div.innerHTML = text;
            
                    //console.log(files[i].name, text);
                    console.log(files[i].name);
                    
                    
                    if (i++ < files.length - 1) {
                        reader.readAsText(files[i]);
                    } else {
                        console.log('done');
                        
                    }
                };
                
                reader.readAsText(files[i]);
            
            }, false);
            
            function copyToClip(str,fileName) {
                function listener(e) {
                e.clipboardData.setData("text/html", str);
                e.clipboardData.setData("text/plain", str);
                e.preventDefault();
                }
                document.addEventListener("copy", listener);
                document.execCommand("copy");
                document.removeEventListener("copy", listener);
                alert("Copied transcript to clipboard:\n'"+fileName+"'");
            };     
            </script>
tpxzln5u

tpxzln5u1#

对于这个问题,添加下面这行代码是有效的:

text = text.replace(/\n?\d*?\n?^.* --> [012345]{2}:.*$/mg ,"");
u4dcyp6a

u4dcyp6a2#

我建议使用replace来代替replace,使用split根据换行符拆分字符串,然后得到一个数组,在其中可以根据需要构造字符串。

let text = `19
00:01:05,100 --> 00:01:08,820
countries such as Spain. Another factor to

20
00:01:08,820 --> 00:01:11,850
consider is the southern tip of Spain's coast

21
00:01:11,850 --> 00:01:15,060
being so close to northern Africa could have`
let newtext = text.split('\n').filter(el => el !== '' && !el.includes('-->') && el.match(/[^A-Za-z0-9\-_]/) );
console.log(newtext);
console.log('Just for example:');
console.log(`${newtext[0]} ${newtext[1]} ${newtext[2]}`);

参考编号:

hrysbysz

hrysbysz3#

您可以删除以可选空行开头的所有文本,然后是只有一个整数的行,最后是以“--〉”分隔时间戳的单行(包括任何终止换行符)。
可选的WEBVTT行不是原始crt标准的一部分,但可以在同一遍中删除:

function cleanCrt(text) {
    return text.replace(/^(WEBVTT\b.*)?[\r\n]*\d+(?:\r\n?|\n)[\d:,.]* --> [\d:,.]*[\r\n]*/gm, "");
}

const text = `WEBVTT
1
00:00:00,940 --> 00:00:04,630
Donkeys were first domesticated around 6000 years

2
00:00:04,630 --> 00:00:08,620
ago in northern Africa and Egypt, primarily for

3
00:00:08,620 --> 00:00:12,820
their milk and their meat. And around 2000 years

4
00:00:12,820 --> 00:00:15,970
ago, donkeys were used as draft animals, carrying 

5
00:00:15,970 --> 00:00:19,720
silk from the Pacific Ocean to the Mediterranean

6
00:00:19,720 --> 00:00:23,350
along the silk route. This was in return for trade [...]

19
00:01:05,100 --> 00:01:08,820
countries such as Spain. Another factor to

20
00:01:08,820 --> 00:01:11,850
consider is the southern tip of Spain's coast

21
00:01:11,850 --> 00:01:15,060
being so close to northern Africa could have`;

console.log(cleanCrt(text));

相关问题