Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all text before a certain point

$(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?

like image 455
UserIsCorrupt Avatar asked May 13 '12 02:05

UserIsCorrupt


1 Answers

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/​

like image 93
VisioN Avatar answered Sep 23 '22 03:09

VisioN