John
John

Reputation: 1

(jQuery, RegEx) Remove from the beginning to some word

Need a simple RegEx for text replacement: from the beginning to the only known word.

Before:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

After:

elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

The only static word is adipisicing so everything else is variable.

I've used this RegEx:

$(this).html($(this).html().replace(/^[^adipisicing]*(adipisicing)/,''));

Works fine.. but I'm sure there is a shorter way!

Upvotes: 0

Views: 698

Answers (4)

VisioN
VisioN

Reputation: 145368

Without regular expressions:

var word = "adipisicing";

$(this).html(function(i, val) {
    return val.substring(val.indexOf(word) + word.length);
});

Using regular expression:

$(this).html(function(i, val) {
    return val.replace(/^.*\badipisicing\b/, "");
});

Here \b will bound a word but not a set of characters.

DEMO: http://jsfiddle.net/rVuh7/

Upvotes: 0

Tats_innit
Tats_innit

Reputation: 34107

Try this: http://jsfiddle.net/Kh7aD/

since indexOf is already in one of the post try using split.

Hope this will help you!

code

var str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";


str = str.split('adipisicing')[1];

Upvotes: 1

Engineer
Engineer

Reputation: 48793

You may try like this:

var tmphtml = $(this).html();
var word = "adipisicing";
$(this).html( tmphtml.substr(tmphtml.indexOf(word)+word.length) );

Upvotes: 0

burning_LEGION
burning_LEGION

Reputation: 13450

use this regex ^.*adipisicing

Upvotes: 0

Related Questions