0
var input = document.getElementById('textinput').value;
var lines = input.split('\n');
var output = '';       
$.each(lines, function(key, line) {

    for(var iii=0; iii<=key; iii++) //for each line
    {
        var filenameRegex = /^\* \[\[Media:(.+?)(\|)/;
        var results = lines[iii].match(filenameRegex);
        var filename;
        console.log('lines[iii]= '+lines[iii]);
            if(results!==null && results.length!== 0)
            {
                  output += lines[iii].replace(filenameRegex,'$1'); 
            }
    }

I try hard but the output is always output += lines[iii].replace(filenameRegex,'$1$2') even though I only want $1

Example input: * [[Media:importantstuff|unimportantstuff]]

Expected output: importantstuff

Actual output: importantstuffunimportantstuff]]

4

1 回答 1

1

如果我理解正确,您正在寻找这个:

演示

代码:

var filenameRegex = /^\* \[\[Media:(.+?)\|.*/;
var results = lines[iii].match(filenameRegex);
var filename;
console.log('lines[' + iii + ']= ' + lines[iii]);
console.log('key[' + iii + ']= ' + key);
if (results !== null && results.length !== 0) {
    output += lines[iii].replace(filenameRegex, '$1');
}
于 2013-07-12T09:38:37.483 回答