Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If substring in string, remove it through the end from string

I'm trying to figure out how to do the following with javascript:
If a substring is in the string, remove from the beginning of the substring till the end of the string from the string.

For example (pseudocode):

var mySub = 'Foo'
var myString = 'testingFooMiscText'
var myString2 = 'testingMisctext'

var myStringEdit = //myString - (Foo till end myString)
var myString2Edit = myString2 //(cause no Foo in it)
like image 326
dmr Avatar asked Aug 11 '11 20:08

dmr


2 Answers

var index = str.indexOf(str1);
if(index != -1)
    str = str.substr(index) 
like image 151
hungryMind Avatar answered Dec 15 '22 01:12

hungryMind


If I understand what you're asking, you'll want to do this:

function replaceIfSubstring(original, substr) {
    var idx = original.indexOf(substr);
    if (idx != -1) {
        return original.substr(idx);
    } else {
        return original;
    }
}
like image 43
FishBasketGordo Avatar answered Dec 15 '22 01:12

FishBasketGordo