$(function(){
    var alphabet = "ABCDEFGHIJKLMNOPQRSTUVEXYZ";
    var replaced = alphabet.replace(/(M).+$/,'');
    $('body').text(replaced);
});
How can I make this go in the opposite direction, replacing M and everything before it?
Use /^.+M/ expression:
$(function() {
    var alphabet = "ABCDEFGHIJKLMNOPQRSTUVEXYZ";
    var replaced = alphabet.replace(/^.+M/,'');
    $('body').text(replaced);
});
DEMO: http://jsfiddle.net/kbZhU/1/
The faster option is to use indexOf and substring methods:
$(function(){
    var alphabet = "ABCDEFGHIJKLMNOPQRSTUVEXYZ";
    var replaced = alphabet.substring(alphabet.indexOf("M") + 1);
    $('body').text(replaced);
});
DEMO: http://jsfiddle.net/kbZhU/2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With