Javascript - Removing part of string.
<p>I've got a script which takes the filename from a file input on a form and puts it into a text field after stripping away some details which aren't required.</p>

<p>All files I upload will be labeled with a key word</p>

<p>Note that this is for a Chrome extension so X-browser support isn't necessary. </p>

<pre><code>var filename = $("uploaded_data").val().toLowerCase();

filename = filename.replace(/_/g, " ").replace(/-/g, " ").replace("c:\\fakepath\\", "");

    type_index[1] = filename.indexOf("red"); 
    type_index[2] = filename.indexOf("blue");
    type_index[3] = filename.indexOf("green");
    type_index[4] = filename.indexOf("purple");
    type_index[5] = filename.indexOf("magenta");

    for (i=0 ; i &gt; -1 ; i++ ) {
        if (type_index[i] &gt; -1)
        {

        filename_final = filename.substring(type_index[i]);
        break;
        }
    }

$("#material_title").val(filename_final);
</code></pre>

<p>Now this code works fine, however, I don't want the colour to be part of the file name.
For example, if the input file was called 'test_red_name_low.jpg' the text field should be 'name low.jpg'. Currently, the code above outputs 'red name low.jpg'. Other times, the filename might be 'this_is_a_test-blue_happy.jpg', which should output 'happy.jpg'.</p>

<p>The type_index array will eventually hold a very large number of values so a replace would be a very long winded way of doing it.</p>

<p>Any suggestions on a way around this?</p>
