Javascript jQuery 从字符串中删除字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5095000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Remove string from string
提问by Carl Markham
I am trying to remove a string from a string in jQuery.
我正在尝试从 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.
我想username1,
从这个列表中删除。我尝试使用将列表添加到数组中,.split(', ')
但出现错误。我假设错误是因为不是每个单词后面都有逗号。
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.
我总是想从列表中删除第一个项目。username1
只是一个示例用户名。如果他们喜欢这篇文章,第一项将始终是当前登录用户的用户名。
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.
但这并没有更新列表。但是,它会在使用.text()
而不是更新列表时更新列表,.html()
但我需要保留列表中的链接。
回答by nathan gonzalez
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(...)
回答by Malloc
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:
要添加 nathan gonzalez 的答案,请注意您需要在调用replace
函数后分配被替换的对象,因为它不是一个 mutator 函数:
myString = myString.replace('username1','');
回答by Mark Coleman
If you just want to remove "username1" you can use a simple replace.
如果您只想删除“username1”,您可以使用简单的替换。
name.replace("username1,", "")
name.replace("username1,", "")
or you could use split like you mentioned.
或者你可以像你提到的那样使用 split 。
var name = "username1, username2 and username3 like this post.".split(",")[1];
$("h1").text(name);
jsfiddleexample
jsfiddle示例
回答by eykanal
I assume that the text "username1" is just a placeholder for what will eventually be an actual username. Assuming that,
我假设文本“username1”只是最终将成为实际用户名的占位符。假如说,
- If the username is notallowed 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 isallowed 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.
- 如果用户名是不是不允许有空格,则只需搜索一切第一空格或逗号之前(从而找到既“U1喜欢这个”,“U1,U2和U3这样”)。
- 如果被允许有一个空间,它可能会更容易包裹在它自己的每个用户名
span
标签的服务器端,将其发送到客户端之前,然后只需用span标签的工作。
回答by Sam Alexander
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
它需要从第一个逗号到结尾的子字符串