最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - If substring in string, remove it through the end from string - Stack Overflow

programmeradmin4浏览0评论

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)

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)
Share Improve this question asked Aug 11, 2011 at 20:41 dmrdmr 22.3k37 gold badges104 silver badges139 bronze badges 5
  • So should the edited "testingFooMiscText" end up as "testing" or as "FooMiscText"? – yoozer8 Commented Aug 11, 2011 at 20:43
  • @dmr To clarify, you would want 'testing' from myString, correct? – Charmander Commented Aug 11, 2011 at 20:44
  • So, if 'Foo' is in the string, you want to truncate the string starting at Foo? Is that what you're asking? – FishBasketGordo Commented Aug 11, 2011 at 20:45
  • @Charmander is correct. I want to end up with 'testing' – dmr Commented Aug 11, 2011 at 20:45
  • A super awesome tidbit for indexOf: Use ~ as invert: if(~index) str = str.substr(index) -awesome read: timmywillison.com/pres/operators/#bitwise-operators - check out preceding slides for more context. – David Hobs Commented Sep 9, 2012 at 5:17
Add a comment  | 

7 Answers 7

Reset to default 6
var index = str.indexOf(str1);
if(index != -1)
    str = str.substr(index) 

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;
    }
}

If you want "testingFooMiscText" to end up as "testing", use

word = word.substring(0, word.indexOf("Foo"));

If you want "testingFooMiscText" to end up as "FooMiscText", use

word = word.substring(word.indexOf("Foo"));

You may need a +/- 1 after the indexOf() to adjust the start/end of the string

myString.substring(0, myString.indexOf(mySub))
var newString = mystring.substring(mystring.indexOf(mySub));

This should do the trick.

var myString = 'testingFooMiscText'
myString.substring(myString.indexOf('Foo'))  //FooMiscText
myString.substring(myString.indexOf('Bar'))  //testingFooMiscText

I used following code to eliminate fakefile from file Name and it worked.

function confsel()
{
    val = document.frm1.fileA.value;
    value of val comes like C:\fakepath\fileName
    var n = val.includes("fakepath");
    if(n)
    {
        val=val.substring(12);
    } 
}
发布评论

评论列表(0)

  1. 暂无评论