user4353985
user4353985

Reputation:

How to replace string with its start and end position

I have string like "Hello Bill Gates" and I have a function which return name position for example if string is "Hello Bill Gates" then it will track name from database and return 7 as start point and 16 as end point now what I want is if someone press BACKSPACE after Bill Gates I get this positions and I want to remove full Gates from string and if someone press BACKSPACE after Bill then it should remove full Bill

Its just like facebook tags if you try to remove tag it will remove your first/last name only first.

so can we remove first / last name from positions ?

What I have tried is :

$(document).keyup(function(){
$('.string').val().length;
var string = $('.string').val().replace('Bill Gates',$('.string').val().split(' ')[0]);
});

Upvotes: 1

Views: 1507

Answers (2)

Vikrant
Vikrant

Reputation: 5036

You can try below code for any strings on your page:

$('.element span').each(function() {
    var text = $(this).text().replace($(this).text().substr(0,$(this).text().indexOf(' ')), '');
    $(this).text(text);
});

It removes first word from string and replaces text with rest of string.

WORKING DEMO

Upvotes: 0

Hamza Kubba
Hamza Kubba

Reputation: 2269

Something like this:

var words = $('.string').val().split(' ');
delete words[words.length - 1];
$('.string').val(words.join(''));

Upvotes: 1

Related Questions