jQuery Remove string from string

JavascriptJqueryString

Javascript Problem Overview


I am trying to remove a string from a string in jQuery.

Here is the string:

username1, username2 and username3 like this post.

I would like to remove username1, from this list. I tried adding the list to an array using .split(', ') but I got an error. I'm assuming the error is because not every word has a comma after it.

I always want to remove the fist item from the list. username1 is just an example username. The first item will always be the username of the currently logged in user if they have liked this post.

I have tried:

  var updated_list = $('#post_like_list').html().replace('username1, ', '');
  $('#post_like_list').html(updated_list);

But that didn't update the list. It does however update the list when using .text() instead of .html() but I have links inside the list which I need to preserve.

Javascript Solutions


Solution 1 - Javascript

pretty sure you just want the plain old replace function. use like this:

myString.replace('username1','');

i suppose if you want to remove the trailing comma do this instead:

myString.replace('username1,','');

edit:

here is your site specific code:

jQuery("#post_like_list-510").text().replace(...) 

Solution 2 - Javascript

To add on nathan gonzalez answer, please note you need to assign the replaced object after calling replace function since it is not a mutator function:

myString = myString.replace('username1','');

Solution 3 - Javascript

If you just want to remove "username1" you can use a simple replace.

name.replace("username1,", "")

or you could use split like you mentioned.

var name = "username1, username2 and username3 like this post.".split(",")[1];      
$("h1").text(name);

jsfiddle example

Solution 4 - Javascript

I assume that the text "username1" is just a placeholder for what will eventually be an actual username. Assuming that,

  • If the username is not allowed to have spaces, then just search for everything before the first space or comma (thus finding both "u1 likes this" and "u1, u2, and u3 like this").
  • If it is allowed to have a space, it would probably be easier to wrap each username in it's own span tag server-side, before sending it to the client, and then just working with the span tags.

Solution 5 - Javascript

Pretty sure nobody answer your question to your exact terms, you want it for dynamic text

var newString = myString.substring( myString.indexOf( "," ) +1, myString.length );

It takes a substring from the first comma, to the end

Solution 6 - Javascript

This is how I coded for removing backslash using jQuery.

// Remove backslash from menu
let selector = jQuery('.crm-submenu-manage-invoices a');

// Get html
let stringRemove = selector.html();

// Remove backslash
stringRemove = stringRemove.replace('\\', '');

// Push back to selector
selector.html(stringRemove);

Solution 7 - Javascript

Adding to Shuvo's answer ... I needed to remove the title from an excerpt and used the following:

a.title is the text I wanted removed.
span.excerpt is where I wanted it removed FROM

var title=$('a.title').text();
let selector = jQuery('span.excerpt');
let stringRemove = selector.html();
stringRemove = stringRemove.replace(title, '');
selector.html(stringRemove);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCjmarkhamView Question on Stackoverflow
Solution 1 - Javascriptnathan gonzalezView Answer on Stackoverflow
Solution 2 - JavascriptMallocView Answer on Stackoverflow
Solution 3 - JavascriptMark ColemanView Answer on Stackoverflow
Solution 4 - JavascripteykanalView Answer on Stackoverflow
Solution 5 - JavascriptSam AlexanderView Answer on Stackoverflow
Solution 6 - JavascriptAkhtarujjaman ShuvoView Answer on Stackoverflow
Solution 7 - JavascriptChad ElkinsView Answer on Stackoverflow