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]]

6
  • Also, there is no need to do for loop, since you already have $.each(lines.... Commented Jul 12, 2013 at 9:28
  • Not the problem, but just in case: using /^* [[Media:(.+?)(\|)/ in replace will only replace the first occurence, add a "g" letter at the end of the regex ot make it global. Commented Jul 12, 2013 at 9:29
  • full code here codepen.io/raindrop11/pen/riBIw tell me if you can't access it Commented Jul 12, 2013 at 9:29
  • Why don't you just remove the 2nd pair of parenthesis if you don't want any $2 captured ? Commented Jul 12, 2013 at 9:32
  • The input is * [[Media:importantstuff|unwantedstuff]] on each line Commented Jul 12, 2013 at 9:34

1 Answer 1

1

If I understood you correctly, you are looking for this:

Demo

Code:

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');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.