William
William

Reputation: 1069

How to replace all from a certain letter?

I want to be able to replace from a letter to the end or to the start.

Lets say i have a string: i love apples i want to be able to search for a which is the beginning of apples and from there replace everything before it! So i am left with apples. Same goes for, search for a and replace everything from a upwards so i am left with i love

My JS only finds word and delete, how do i make it do the above? Thanks!

JS:

var queuestring2 = $(this).html();
queuestring2 = queuestring2.replace("path", "");
$(this).html(queuestring2);

Upvotes: 2

Views: 65

Answers (1)

Weedoze
Weedoze

Reputation: 13943

var myString = "I love apples";
var letter = "a";
var index = myString.indexOf(letter);

//Remove everything before 'a'
var endString = myString.slice(index);

//Remove everything after 'a'
var startString = myString.slice(0, index);

console.log("Remove before -"+letter+"- : " + endString);
console.log("Remove after -"+letter+"- : " + startString);

Upvotes: 3

Related Questions