var myStr = "I love chocolate and strawberry, love this and that as well, and again love walking along the street";
var newStr = myStr.substr(myStr.indexOf('love'), myStr.lastIndexOf('love'));
///" chocolate and strawberry, "; //this is the output
get substring entre deux indices en utilisant indexOf()
var myStr = "I love chocolate and strawberry, love this and that as well, and again love walking along the street";
var newStr = myStr.substr(myStr.indexOf('love'), myStr.lastIndexOf('love'));
console.log(newStr);
Comment puis-je obtenir le texte entre le premier mot « amour » et le second mot « amour » qui devrait être "chocolat et la fraise, "?
'String.indexOf()' prend en charge deuxième param, 'fromIndex' [(doc)] (https://developer.mozilla.org/en/docs/Web/JavaScript/ Référence/Global_Objects/String/indexOf). Si vous stockez le résultat du premier 'indexOf', il suffit de le réutiliser. – raina77ow